If you have read my previous article and added TSLint support to your React App, at some point you will have to migrate to ESLint for the reason that TSLint is going to be deprecated (which I also mentioned in “Set Up TSLint and Prettier in VS Code for React App with Typescript”).
Below I describe the steps for successful migration, along with providing file configurations to make it easier for you to jump right into using ESLint in your React App with Typescript. Enjoy!
Your package.json should contain at least the versions below:
in your package.json specify the script:
“lint:ts”: “eslint --ext=jsx,ts,tsx src”
yarn remove @typescript-eslint/eslint-plugin-tslint tslint tslint-config-prettier tslint-react
⚠️ ESLint is coming with Create-React-App, so you can skip this step.
You can install ESLint using yarn or npm:
npm install eslint --save-dev
yarn add eslint --dev
You should then set up a configuration file:
$ ./node_modules/.bin/eslint --init
You will have to go through prompt and choose what will go into the auto-generated .eslintrc.js file.
for more details check this link: Configuring ESLint
Of course, you will still have to customize this file if you want to have typescript, react and prettier support.
So, this is pretty much the main config file. It contains the information about which configs, plugins for linting you are using. You can tweak the recommended rules from here as well.
If you don’t want to lint certain files, then at the root level of your application’s folder structure you can add a .eslintignore file, where you can list any files that should be ignored.
And to support React-related rules you can install eslint-plugin-react, you can find the list of all rules here
- install the plugin:
npm install eslint-plugin-react --save-dev
yarn add eslint-plugin-react --dev
- the presets to get reasonable defaults:
On top of that you can add a plugin to lint react hooks:
npm install eslint-plugin-react-hooks --save-dev
yarn add eslint-plugin-react-hooks --dev
enable your react-hooks plugin in .eslintrc.js:
"plugins": ["@typescript-eslint", "react-hooks"]
the rules that it activates:
Now that you already have eslint and you would like to extends its capabilities to typescript, install the following to the dev dependencies:
yarn add @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev
npm i @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
@typescript-eslint/parser: eslint-specific parser that understands typescript AST format
@typescript-eslint/eslint-plugin: provides a good dose of typescript-specific eslint rules
This file contains essential typescript rules like noImplicitAny, etc.
Now that you have your eslint-typescript symbiosis you might want to enforce the auto-fixing of code formatting.
yarn add prettier eslint-config-prettier eslint-plugin-prettier --dev
- prettier: the core prettier library
- eslint-config-prettier: disables eslint rules that might conflict with prettier
in your .eslintrc.js:
This will enable eslint-plugin-prettier, set prettier/prettier rule to “error”, and extend the eslint-config-prettier configuration.
- eslint-plugin-prettier: runs prettier as an eslint rule
I specified my prettier rules inside .eslintrc.js file above and it works for me to have all the different sorts of rules in one place. However, it is also possible to have them in a separate .prettierrc file if you’d like.
The whole list of prettier’s code formatting options can be found here
So, in my .eslitrc.js file on line 17 I have commented out “plugin:prettier/recommended”, which fully enables prettier plugin, and remember that it is very opinionated, and might conflict with your eslint rules.
download and install vscode prettier extension from here
open VSCode, hit ‘⇧⌘P’ to open the Command Palette… and type in ‘Preferences: Open Settings (JSON)’ and enable eslint auto-fixing like below on line 25–36:
To fix your lint errors run this command:
npm run lint:ts --fix
yarn run lint:ts --fix
That’s all Folks! ☕️