Overview
@typescript-eslint/eslint-plugin
includes over 100 rules that detect best practice violations, bugs, and/or stylistic issues specifically for TypeScript code. All of our rules are listed below.
Instead of enabling rules one by one, we recommend using one of our pre-defined configs to enable a large set of recommended rules.
Rulesβ
The rules are listed in alphabetical order. You can optionally filter them based on these categories:
(These categories are explained in more detail below.)
Rule | βοΈ | π§ | π | π§± | π |
---|---|---|---|---|---|
Require that function overload signatures be consecutive | π¨ | ||||
Require consistently using either T[] or Array<T> for arrays | π¨ | π§ | |||
Disallow awaiting a value that is not a Thenable | β | π‘ | π | ||
Disallow @ts-<directive> comments or require descriptions after directives | β | π‘ | |||
Disallow // tslint:<rule-flag> comments | π¨ | π§ | |||
Enforce that literals on classes are exposed in a consistent style | π¨ | π‘ | |||
Enforce that class methods utilize this | π§± | ||||
Enforce specifying generic type arguments on type annotation or constructor name of a constructor call | π¨ | π§ | |||
Require or disallow the Record type | π¨ | π§ π‘ | |||
Require return statements to either always or never specify values | π | π§± | |||
Enforce consistent usage of type assertions | π¨ | π§ π‘ | |||
Enforce type definitions to consistently use either interface or type | π¨ | π§ | |||
Enforce consistent usage of type exports | π§ | π | |||
Enforce consistent usage of type imports | π§ | ||||
Enforce default parameters to be last | π§± | ||||
Enforce dot notation whenever possible | π¨ | π§ | π | π§± | |
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 or disallow initialization in variable declarations | π§± | ||||
Enforce a maximum number of parameters in function definitions | π§± | ||||
Require a consistent member declaration order | |||||
Enforce using a particular method signature syntax | π§ | ||||
Enforce naming conventions for everything across a codebase | π | ||||
Disallow generic Array constructors | β | π§ | π§± | ||
Disallow using the delete operator on array values | β | π‘ | π | ||
Require .toString() and .toLocaleString() to only be called on objects which provide useful information when stringified | β | π | |||
Disallow non-null assertion in locations that may be confusing | π¨ | π‘ | |||
Require expressions of type void to appear in statement position | π | π§ π‘ | π | ||
Disallow using code marked as @deprecated | π | π | |||
Disallow duplicate class members | π§± | ||||
Disallow duplicate enum member values | β | ||||
Disallow duplicate constituents of union or intersection types | β | π§ | π | ||
Disallow using the delete operator on computed key expressions | π | π§ | |||
Disallow empty functions | π¨ | π§± | |||
Disallow the declaration of empty interfaces | π§ π‘ | π | |||
Disallow accidentally using the "empty object" type | β | π‘ | |||
Disallow the any type | β | π§ π‘ | |||
Disallow extra non-null assertions | β | π§ | |||
Disallow classes used as namespaces | π | ||||
Require Promise-like statements to be handled appropriately | β | π‘ | π | ||
Disallow iterating over an array with a for-in loop | β | π | |||
Disallow the use of eval() -like functions | β | π | π§± | ||
Enforce the use of top-level import type qualifier when an import only has specifiers with inline type qualifiers | π§ | ||||
Disallow explicit type declarations for variables or parameters initialized to a number, string, or boolean | π¨ | π§ | |||
Disallow this keywords outside of classes or class-like objects | π§± | ||||
Disallow void type outside of generic or return types | π | ||||
Disallow function declarations that contain unsafe references inside loop statements | π§± | ||||
Disallow literal numbers that lose precision | π§± | π | |||
Disallow magic numbers | π§± | ||||
Disallow the void operator except when used to discard a value | π | π§ π‘ | π | ||
Enforce valid definition of new and constructor | β | ||||
Disallow Promises in places not designed to handle them | β | π | |||
Disallow using the spread operator when it might cause unexpected behavior | π | π‘ | π | ||
Disallow enums from having both number and string members | π | π | |||
Disallow TypeScript namespaces | β | ||||
Disallow non-null assertions in the left operand of a nullish coalescing operator | π | π‘ | |||
Disallow non-null assertions after an optional chain expression | β | π‘ | |||
Disallow non-null assertions using the ! postfix operator | π | π‘ | |||
Disallow variable redeclaration | π§± | ||||
Disallow members of unions and intersections that do nothing or override type information | β | π | |||
Disallow invocation of require() | β | ||||
Disallow specified modules when loaded by import | π§± | ||||
Disallow certain types | π§ π‘ | ||||
Disallow variable declarations from shadowing variables declared in the outer scope | π§± | ||||
Disallow aliasing this | β | ||||
Disallow type aliases | π | ||||
Disallow unnecessary equality comparisons against boolean literals | π | π§ | π | ||
Disallow conditionals where the type is always truthy or always falsy | π | π‘ | π | ||
Disallow unnecessary assignment of constructor property parameter | |||||
Disallow unnecessary namespace qualifiers | π§ | π | |||
Disallow unnecessary template expressions | π | π§ | π | ||
Disallow type arguments that are equal to the default | π | π§ | π | ||
Disallow type assertions that do not change the type of an expression | β | π§ | π | ||
Disallow unnecessary constraints on generic types | β | π‘ | |||
Disallow conversion idioms when they do not change the type or value of the expression | π | π‘ | π | ||
Disallow type parameters that aren't used multiple times | π | π‘ | π | ||
Disallow calling a function with a value with type any | β | π | |||
Disallow assigning a value with type any to variables and properties | β | π | |||
Disallow calling a value with type any | β | π | |||
Disallow unsafe declaration merging | β | ||||
Disallow comparing an enum value with a non-enum value | β | π‘ | π | ||
Disallow using the unsafe built-in Function type | β | ||||
Disallow member access on a value with type any | β | π | |||
Disallow returning a value with type any from a function | β | π | |||
Disallow type assertions that narrow a type | π | ||||
Require unary negation to take a number | β | π | |||
Disallow unused expressions | β | π§± | |||
Disallow unused variables | β | π§± | |||
Disallow the use of variables before they are defined | π§± | ||||
Disallow unnecessary constructors | π | π‘ | π§± | ||
Disallow empty exports that don't change anything in a module file | π§ | ||||
Disallow require statements except in import statements | π | ||||
Disallow using confusing built-in primitive class wrappers | β | π§ | |||
Enforce non-null assertions over explicit type assertions | π¨ | π§ | π | ||
Disallow throwing non- Error values as exceptions | β | π | π§± | ||
Require or disallow parameter properties in class constructors | |||||
Enforce the use of as const over literal type | β | π§ π‘ | |||
Require destructuring from arrays and/or objects | π§ | π | π§± | ||
Require each enum member value to be explicitly initialized | π‘ | ||||
Enforce the use of Array.prototype.find() over Array.prototype.filter() followed by [0] when looking for a single result | π¨ | π‘ | π | ||
Enforce the use of for-of loop over the standard for loop where possible | π¨ | ||||
Enforce using function types instead of interfaces with call signatures | π¨ | π§ | |||
Enforce includes method over indexOf method | π¨ | π§ | π | ||
Require all enum members to be literal values | π | ||||
Require using namespace keyword over module keyword to declare custom TypeScript modules | β | π§ | |||
Enforce using the nullish coalescing operator instead of logical assignments or chaining | π¨ | π‘ | π | ||
Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects | π¨ | π§ π‘ | π | ||
Require using Error objects as Promise rejection reasons | β | π | π§± | ||
Require private members to be marked as readonly if they're never modified outside of the constructor | π§ | π | |||
Require function parameters to be typed as readonly to prevent accidental mutation of inputs | π | ||||
Enforce using type parameter when calling Array#reduce instead of using a type assertion | π | π§ | π | ||
Enforce RegExp#exec over String#match if no global flag is provided | π¨ | π§ | π | ||
Enforce that this is used when only this type is returned | π | π§ | π | ||
Enforce using String#startsWith and String#endsWith over other equivalent methods of checking substrings | π¨ | π§ | π | ||
Enforce using @ts-expect-error over @ts-ignore | π§ | π | |||
Require any function or method that returns a Promise to be marked async | π§ | π | |||
Enforce that get() types should be assignable to their equivalent set() type | π | π | |||
Require Array#sort and Array#toSorted calls to always provide a compareFunction | π | ||||
Disallow async functions which do not return promises and have no await expression | β | π‘ | π | π§± | |
Require both operands of addition to be the same type and be bigint , number , or string | β | π | |||
Enforce template literal expressions to be of string type | β | π | |||
Enforce consistent awaiting of returned promises | π | π§ π‘ | π | ||
Enforce constituents of a type union/intersection to be sorted alphabetically | π§ π‘ | π | |||
Disallow certain types in boolean expressions | π‘ | π | |||
Require switch-case statements to be exhaustive | π‘ | π | |||
Disallow certain triple slash directives in favor of ES6-style import declarations | β | ||||
Require type annotations in certain places | π | ||||
Enforce unbound methods are called with their expected scope | β | π | |||
Disallow two overloads that could be unified into one with a union or an optional/rest parameter | π | ||||
Enforce typing arguments in Promise rejection callbacks as unknown | π | π‘ | π |
Filteringβ
Config Group (βοΈ)β
"Config Group" refers to the pre-defined config that includes the rule. Extending from a configuration preset allow for enabling a large set of recommended rules all at once.
Metadataβ
π§ fixable
refers to whether the rule contains an ESLint--fix
auto-fixer.π‘ has suggestions
refers to whether the rule contains an ESLint suggestion fixer.- Sometimes, it is not safe to automatically fix the code with an auto-fixer. But in these cases, we often have a good guess of what the correct fix should be, and we can provide it as a suggestion to the developer.
π requires type information
refers to whether the rule requires typed linting.π§± extension rule
means that the rule is an extension of an core ESLint rule (see Extension Rules).π deprecated rule
means that the rule should no longer be used and will be removed from the plugin in a future version.
Extension Rulesβ
Some core ESLint rules do not support TypeScript syntax: either they crash, ignore the syntax, or falsely report 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.
Extension rules generally completely replace the base rule from ESLint core. If the base rule is enabled in a config you extend from, you'll need to disable the base rule:
module.exports = {
extends: ['eslint:recommended'],
rules: {
// Note: you must disable the base rule as it can report incorrect errors
'no-unused-vars': 'off',
'@typescript-eslint/no-unused-vars': 'error',
},
};
Search for π§± extension rule
s in this page to see all extension rules.
Frozen Rulesβ
When rules are feature complete, they are marked as frozen (indicated with βοΈ in the documentation). This applies to standalone rules that are complete, as well as extension rules whose underlying core ESLint rules are frozen. After that point, we expect users to use disable comments when they find an edge case that isnβt covered.
When a rule is frozen, it means:
- Bug fixes: We will still fix confirmed bugs.
- New ECMAScript features: We will ensure compatibility with new ECMAScript features, meaning the rule will not break on new syntax.
- TypeScript support: We will ensure compatibility with TypeScript syntax, meaning the rule will not break on TypeScript syntax and violations are appropriate for TypeScript.
- New options: We will not add any new options unless an option is the only way to fix a bug or support a newly-added ECMAScript feature.
If you find that a frozen rule would work better for you with a change, we recommend copying the rule source code and modifying it to fit your needs.