ESLint Plugin TypeScript

Azure Pipelines GitHub license NPM Version NPM Downloads Commitizen friendly

Getting Started

These docs walk you through setting up ESLint, this plugin, and our parser. If you know what you're doing and just want to quick start, read on...

Quick-start

Installation

Make sure you have TypeScript and @typescript-eslint/parser installed, then install the plugin:

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

It is important that you use the same version number for @typescript-eslint/parser and @typescript-eslint/eslint-plugin.

Note: If you installed ESLint globally (using the -g flag) then you must also install @typescript-eslint/eslint-plugin globally.

Usage

Add @typescript-eslint/parser to the parser field and @typescript-eslint to the plugins section of your .eslintrc configuration file, then configure the rules you want to use under the rules section.

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/rule-name": "error"
  }
}

You can also enable all the recommended rules for our plugin. Add plugin:@typescript-eslint/recommended in extends:

{
  "extends": ["plugin:@typescript-eslint/recommended"]
}

Note: Make sure to use eslint --ext .js,.ts since by default eslint will only search for .js files.

You can also use eslint:recommended (the set of rules which are recommended for all projects by the ESLint Team) with this plugin. As noted in the root README, not all ESLint core rules are compatible with TypeScript, so you need to add both eslint:recommended and plugin:@typescript-eslint/eslint-recommended (which will adjust the one from ESLint appropriately for TypeScript) to your config:

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended"
  ]
}

As of version 2 of this plugin, by design, none of the rules in the main recommended config require type-checking in order to run. This means that they are more lightweight and faster to run.

Some highly valuable rules simply require type-checking in order to be implemented correctly, however, so we provide an additional config you can extend from called recommended-requiring-type-checking. You would apply this in addition to the recommended configs previously mentioned, e.g.:

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ]
}

Pro Tip: For larger codebases you may want to consider splitting our linting into two separate stages: 1. fast feedback rules which operate purely based on syntax (no type-checking), 2. rules which are based on semantics (type-checking).

You can read more about linting with type information here

Supported Rules

Key: ✔️ = recommended, 🔧 = fixable, 💭 = requires type information

Name

Description

✔️

🔧

💭

Require that member overloads be consecutive

✔️

Requires using either T[] or Array<T> for arrays

🔧

Disallows awaiting a value that is not a Thenable

✔️

💭

Bans // @ts-<directive> comments from being used

Bans specific types from being used

✔️

🔧

Ensures that literals on classes are exposed in a consistent style

🔧

Enforces consistent usage of type assertions

✔️

Consistent with type definition either interface or type

🔧

Require explicit return types on functions and class methods

✔️

Require explicit accessibility modifiers on class properties and methods

🔧

Require explicit return and argument types on exported functions' and classes' public class methods

Require a specific member delimiter style for interfaces and type literals

✔️

🔧

Require a consistent member declaration order

Enforces using a particular method signature syntax.

🔧

Enforces naming conventions for everything across a codebase

💭

Requires that .toString() is only called on objects which provide useful information when stringified

💭

Disallow the delete operator with computed key expressions

🔧

Disallow the declaration of empty interfaces

✔️

🔧

Disallow usage of the any type

✔️

🔧

Disallow extra non-null assertion

🔧

Forbids the use of classes as namespaces

Requires Promise-like values to be handled appropriately

💭

Disallow iterating over an array with a for-in loop

✔️

💭

Disallow the use of eval()-like methods

💭

Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean

✔️

🔧

Disallows usage of void type outside of generic or return types

Enforce valid definition of new and constructor

✔️

Avoid using promises in places not designed to handle them

✔️

💭

Disallow the use of custom TypeScript modules and namespaces

✔️

Disallows using a non-null assertion after an optional chain expression

Disallows non-null assertions using the ! postfix operator

✔️

Disallow the use of parameter properties in class constructors

Disallows invocation of require()

Disallow aliasing this

✔️

Disallow throwing literals as exceptions

💭

Disallow the use of type aliases

Flags unnecessary equality comparisons against boolean literals

🔧

💭

Prevents conditionals where the type is always truthy or always falsy

🔧

💭

Warns when a namespace qualifier is unnecessary

🔧

💭

Enforces that type arguments will not be used if not required

🔧

💭

Warns if a type assertion does not change the type of an expression

✔️

🔧

💭

Disallows assigning any to variables and properties

💭

Disallows calling an any type value

💭

Disallows member access on any typed variables

💭

Disallows returning any from a function

💭

Disallow unused variables and arguments

💭

Disallows the use of require statements except in import statements

✔️

Prefer usage of as const over literal type

🔧

Prefer a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated

Use function types instead of interfaces with call signatures

🔧

Enforce includes method over indexOf method

✔️

🔧

💭

Require the use of the namespace keyword instead of the module keyword to declare custom TypeScript modules

✔️

🔧

Enforce the usage of the nullish coalescing operator instead of logical chaining

🔧

💭

Prefer using concise optional chain expressions instead of chained logical ands

🔧

Requires that private members are marked as readonly if they're never modified outside of the constructor

🔧

💭

Requires that function parameters are typed as readonly to prevent accidental mutation of inputs

💭

Prefer using type parameter when calling Array#reduce instead of casting

🔧

💭

Enforce that RegExp#exec is used instead of String#match if no global flag is provided

✔️

💭

Enforce the use of String#startsWith and String#endsWith instead of other equivalent methods of checking substrings

✔️

🔧

💭

Recommends using // @ts-expect-error over // @ts-ignore

🔧

Requires any function or method that returns a Promise to be marked async

💭

Requires Array#sort calls to always provide a compareFunction

💭

When adding two variables, operands must both be of type number or of type string

💭

Enforce template literal expressions to be of string type

💭

Restricts the types allowed in boolean expressions

💭

Exhaustiveness checking in switch with union type

💭

Sets preference level for triple slash directives versus ES6-style import declarations

✔️

Require consistent spacing around type annotations

✔️

🔧

Requires type annotations to exist

Enforces unbound methods are called with their expected scope

✔️

💭

Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter

Extension Rules

In some cases, ESLint provides a rule itself, but it doesn't support TypeScript syntax; either it crashes, or it ignores the syntax, or it falsely reports against it. In these cases, we create what we call an extension rule; a rule within our plugin that has the same functionality, but also supports TypeScript.

Key: ✔️ = recommended, 🔧 = fixable, 💭 = requires type information

Name

Description

✔️

🔧

💭

Enforce consistent brace style for blocks

🔧

Enforces consistent spacing before and after commas

🔧

Enforce default parameters to be last

enforce dot notation whenever possible

🔧

💭

Require or disallow spacing between function identifiers and their invocations

🔧

Enforce consistent indentation

🔧

require or disallow initialization in variable declarations

Enforce consistent spacing before and after keywords

🔧

Require or disallow an empty line between class members

🔧

Disallow generic Array constructors

✔️

🔧

Disallow duplicate class members

Disallow empty functions

✔️

Disallow unnecessary parentheses

🔧

Disallow unnecessary semicolons

🔧

disallow this keywords outside of classes or class-like objects

Disallow magic numbers

Disallow unused expressions

Disallow unused variables

✔️

Disallow the use of variables before they are defined

✔️

Disallow unnecessary constructors

Enforce the consistent use of either backticks, double, or single quotes

🔧

Disallow async functions which have no await expression

✔️

💭

Enforces consistent returning of awaited values

🔧

💭

Require or disallow semicolons instead of ASI

🔧

Enforces consistent spacing before function parenthesis

🔧

Contributing

See the contributing guide here.

Last updated