DEV Community

Cover image for A simple way to configure Eslint to TypeScript
Helder Burato Berto
Helder Burato Berto

Posted on • Updated on

A simple way to configure Eslint to TypeScript

On this article, I will show you to configure eslint with typescript in a quick way.

I'll use yarn on the examples below, but it can be done with npm too.

First of all, you need to install eslint in your project by using the following command:

$ yarn add -D eslint
Enter fullscreen mode Exit fullscreen mode

Start the Configuration

To create your configuration file and set TypeScript as enabled, you need to execute the following command in your project:

$ yarn eslint --init
Enter fullscreen mode Exit fullscreen mode

After that, you will need to choose some options for the following questions:

How would you like to use ESLint?

  1. To check syntax only;
  2. To check syntax and find problems;
  3. To check syntax, find problems, and enforce code style;

Choose option 3.

What type of modules does your project use?

  1. JavaScript modules (import/export);
  2. CommonJS (require/exports);
  3. None of these;

Choose option 1 - JavaScript modules (import/export).

Which framework does your project use?

  1. React;
  2. Vue.js;
  3. None of these;

In this case, to use NodeJS + TS, I'll choose option 3 - Node of These.

Does your project use TypeScript?

(y/N) - The option N is selected as a default
Enter fullscreen mode Exit fullscreen mode

To accept TypeScript in your eslint configure, set y.

Where does your code run?

  1. Browser;
  2. Node;

If you enable the two options you can work in both.

In this example, I chose 2 - Node and disabled 1 - Browser.

Note: You can use the spacebar to enable/disable options in this section.

How would you like to define a style for your project?

  1. Use a popular style guide;
  2. Answer questions about your style;
  3. Inspect your JavaScript file(s);

Personally, I like to use 1. Use a popular style guide and Standard.

What format do you want your config file to be in?

  1. JavaScript;
  2. YAML;
  3. JSON;

I like to use the JavaScript format because it's easier when you need to import external files.

Would you like to install them now with npm?

(Y/n) - The option Y is selected as a default
Enter fullscreen mode Exit fullscreen mode

Just press enter to install.

Note: Because I commonly use yarn, I wait to install the packages in this last part. After that, I delete the node_modules and install again using yarn to guarantee consistency.

Wrapping Up

I use this process in some projects of mine and this is the easiest way to configure and use the most updated packages to enable eslint in projects.

Was this process helpful to you? Feel free to comment below.

Top comments (2)

Collapse
 
karranb profile image
Karran Besen

I can't believe I wasn't aware of $ yarn eslint --init until now 🤦

Collapse
 
helderberto profile image
Helder Burato Berto

It's good that this tip was useful to you! ✌🏻