DEV Community

Sam
Sam

Posted on • Edited on

TSLint -> ESLint/CRA/Typescript/Prettier

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!

package.json

Your package.json should contain at least the versions below:
typescript@3.4.5
react-scripts@^3.0.1
react@^16.8.6

script

in your package.json specify the script:

“lint:ts”: “eslint --ext=jsx,ts,tsx src”

remove everything related to TSLint (if you have any)

yarn remove @typescript-eslint/eslint-plugin-tslint tslint tslint-config-prettier tslint-react

install ESLint

⚠️ 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

Set up a configuration file (.eslintrc)

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.

.eslintrc.js

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.

.eslintignore

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.
For example:

src/serviceWorker.ts
src/tests/

eslint with React

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:

"extends": [
"eslint:recommended",
"plugin:react/recommended"
]

On top of that you can add a plugin to lint react hooks:

eslint-plugin-react-hooks

  • install:

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:

"rules": {
// ...
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}

setting up ESLint to work with TypeScript

Why typescript-eslint?

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

tsconfig.json

This file contains essential typescript rules like noImplicitAny, etc.

prettier

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:
{
“extends”: [“plugin:prettier/recommended”]
}

This will enable eslint-plugin-prettier, set prettier/prettier rule to “error”, and extend the eslint-config-prettier configuration.

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.

prettier and VSCode

  • 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! ☕️

Top comments (0)