[Typescript] 0. 개발환경 구성

TODO

Settings for vscode

$ npm init -y
$ yarn add -D @types/node typescript ts-node
$ npx tsconfig.json
// package.json
"scripts": {
    "start": "ts-node src/app.ts"
},
// src/app.ts
console.log('Hello world')
$  yarn start
## 출력
    yarn run v1.22.5
    $ ts-node src/app.ts
    Hello world
    Done in 1.10s.

nodemon 설치

$  yarn add -D nodemon
"scripts": {
    "watch": "tsc -w",
    "dev": "nodemon dist/app.js",
    "start": "node dist/app.ts",
},
$  nodemon --watch 'src/**/*.ts' --exec 'ts-node' src/app.ts"`
"scripts": {
    "dev": "nodemon --watch 'src/**/*.ts' --exec ts-node src/app.ts",
    "start":"node dist/app.js",
    "build": "tsc",
    "build:clean": "yarn clean:pack && yarn clean && yarn build",
    "clean": "rm -rf build",
    "clean:pack": "rm -f *.tgz",
    "lint": "tslint -p .",
    "package": "yarn build:clean && npm pack"
}
$  yarn add -D tslint

ts-node-dev

It restarts target node process when any of required files changes (as standard node-dev) but shares Typescript compilation process between restarts. This significantly increases speed of restarting comparing to node-dev -r ts-node/register …, nodemon -x ts-node … variations because there is no need to instantiate ts-node compilation each time.

절대경로 임포트 하기 ts-config-paths

tsconfig.json 파일 paths 요소 추가

{
  // ....
  "outDir": "./dist",
  "rootDir": "./src",
  "paths": {
      "@/*": ["src/*"]
  },
}

ts-config-paths 설치

yarn add -D ts-config-paths
ts-node -r tsconfig-paths/register main.ts

## 또는
ts-node-dev -r tsconfig-paths/register --respawn --transpile-only --no-notify src/index.ts
// package.json
 "scripts": {
  "prebuild": "rm -rf dist",
  "build": "tsc",
  "dev": "ts-node-dev -r tsconfig-paths/register --respawn --transpile-only --no-notify src/index.ts"
  },