6
7

More than 5 years have passed since last update.

Node.js で TypeScript + TSLint を使う最小構成

Last updated at Posted at 2018-10-04

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'"
  },
}
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7