Updated
Reading time ∼1 mins

Linting with TypeScript ESLint

warning

This post is old. Some information may be inaccurate.

Table of contents

TypeScript ESLint is the successor of TSLint, the linting tool exclusively developed for TypeScript. It leverages the existing JavaScript linting ecosystem and brings consistency across the tooling to lint both JavaScript and TypeScript apps.

In this post, we'll set up linting with TypeScript ESLint for a TypeScript project.

Setup

You can pick the Node.js application created in the post Logging on Node.js with log4js-node to follow this post.

Install dependencies for typescript-eslint

Execute the following commands to add required dependencies for typescript-eslint.

yarn add -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser

Create configuration file

Generate an eslint configuration file with the following command.

npx eslint --init

Replace the contents of .eslintrc.js with the following configuration.

module.exports = {
  'parser': '@typescript-eslint/parser',
  'extends': ['plugin:@typescript-eslint/eslint-recommended'],
  'parserOptions': {
    'ecmaVersion': 2018,
    'sourceType': 'module'
  },
  'plugins': ['@typescript-eslint'],
  'rules': {
    'quotes': ['error', 'single'],
    'semi': ['error', 'always'],
    '@typescript-eslint/explicit-function-return-type': 'off',
    '@typescript-eslint/no-explicit-any': 1,
    '@typescript-eslint/no-inferrable-types': [
      'warn', { 'ignoreParameters': true }
    ],
    '@typescript-eslint/no-unused-vars': 'warn'
  }
}

Edit package.json scripts as follows.

"scripts": {
  "prestart": "npm run build && npm run lint",
  "lint": "eslint src/**/* --fix",
}

You can lint the files using yarn lint. Also, when the application is started with yarn start, the prestart script will launch the build generation and lint it as well.

References

Source codelinting-with-typescript-eslint

Related