|
| 1 | +# ESLint |
| 2 | + |
| 3 | +ESLint existed to lint JavaScript, but no it is also becoming the defacto linter for [TypeScript](https://github.com/Microsoft/TypeScript/issues/29288), thanks to the [collaboration](https://eslint.org/blog/2019/01/future-typescript-eslint) between the two teams. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +To setup ESLint for TypeScript you need the following packages: |
| 8 | + |
| 9 | +```sh |
| 10 | +npm i eslint eslint-plugin-react @typescript-eslint/parser @typescript-eslint/eslint-plugin |
| 11 | +``` |
| 12 | + |
| 13 | +> TIP: eslint calls packages that contain lint rules as "plugin" |
| 14 | +
|
| 15 | +* eslint : Core eslint |
| 16 | +* eslint-plugin-react : For react rules provided by eslint. [Supported rules list](https://github.com/yannickcr/eslint-plugin-react#list-of-supported-rules) |
| 17 | +* @typescript-eslint/parse : To allow eslint to understand ts / tsx files |
| 18 | +* @typescript-eslint/eslint-plugin : For TypeScript rules. [Supported rules list](https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules) |
| 19 | + |
| 20 | +> As you can see there are two eslint packages (for use with js or ts) and two @typescript-eslint packages (for use with ts). So the overhead for TypeScript is not *that much*. |
| 21 | +
|
| 22 | +## Configure |
| 23 | +Create `.eslintrc.js`: |
| 24 | + |
| 25 | +```js |
| 26 | +module.exports = { |
| 27 | + parser: '@typescript-eslint/parser', |
| 28 | + parserOptions: { |
| 29 | + project: './tsconfig.json', |
| 30 | + }, |
| 31 | + plugins: ['@typescript-eslint'], |
| 32 | + extends: [ |
| 33 | + 'plugin:react/recommended', |
| 34 | + 'plugin:@typescript-eslint/recommended', |
| 35 | + ], |
| 36 | + rules: { |
| 37 | + // Overwrite rules specified from the extended configs e.g. |
| 38 | + // "@typescript-eslint/explicit-function-return-type": "off", |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Configure VSCode |
| 44 | + |
| 45 | +* Install extension https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint |
| 46 | +* Add to `settings.json`: |
| 47 | +```js |
| 48 | +"eslint.validate": [ |
| 49 | + "javascript", |
| 50 | + "javascriptreact", |
| 51 | + {"language": "typescript", "autoFix": true }, |
| 52 | + {"language": "typescriptreact", "autoFix": true } |
| 53 | +], |
| 54 | +``` |
0 commit comments