Node.js で TypeScript + TSLint を使う最小構成。
環境
- typescript: 3.1.1
- tslint: 5.11.0
TypeScript
Install
typescript
と @types/node
をインストールする。
npm i -D typescript
npm i @types/node
tsconfig.json
ルートディレクトリに tsconfig.json
を作成し、次のように記述する。
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"sourceMap": true,
"outDir": "dist",
"types": [ "node" ]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
これで、src
ディレクトリ以下にある ts
ファイルをコンパイルし、生成した js
ファイルを dist
ディレクトリ以下に出力する。
src
ディレクトリ以下のディレクトリ構成は dist
ディレクトリでも保持される。
package.json
package.json
を編集し、次のスクリプトを追加する。
{
"scripts": {
"start": "node dist/app.js",
"build": "tsc",
"watch": "tsc --watch"
},
}
TSLint
Install
tslint
をインストールする。
npm i -D tslint
tslint.json
ルートディレクトリに tslint.json
を作成し、次のように記述する。
ルールはお好みで。
{
"defaultSeverity": "warning",
"extends": [
"tslint:recommended"
],
"rules": {
"quotemark": [true, "single"],
"indent": [true, "space", 2],
"interface-name": false,
"ordered-imports": false,
"object-literal-sort-keys": false,
"no-consecutive-blank-lines": false,
"no-console": false
}
}
package.json
package.json
を編集し、次のスクリプトを追加する。
{
"scripts": {
"lint": "tslint 'src/**/*.ts'"
},
}