5つインストールする。 パッケージマネージャに応じてコマンドは変えること。
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-config-prettier prettier
/.eslintrc.json
を作成してください。
{
"env": {
"es2021": true
// 環境に応じてenvは変えてください
// "node": true,
// "browser": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"project": "./tsconfig.json"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
// お好みで設定ください
"@typescript-eslint/no-explicit-any": 1,
"@typescript-eslint/no-non-null-assertion": 1
},
"root": true
}
適宜npm scriptsなどに登録してください。
npx prettier --write 'src/**/*.ts' && eslint --fix --ext .ts src
ESLintの設定ファイルについて主要な部分を解説する。
plugin:@typescript-eslint/recommended
は、@typescript-eslint/eslint-plugin
プラグインに含まれているeslint:recommended
を指定したり、rulesに@typescript-eslint/...
のルールを個別指定したりするのに必要https://www.npmjs.com/package/@typescript-eslint/eslint-plugin
ブログ投稿初めてです。 フィードバックや記事内容の誤りなどありましたら、お気軽にコメントください。