ESLint Plugin TypeScript
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.
Recommended Configs
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
✔️
🔧
💭
Ensures that literals on classes are exposed in a consistent style
🔧
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
✔️
🔧
Requires that .toString()
is only called on objects which provide useful information when stringified
💭
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
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
Flags unnecessary equality comparisons against boolean literals
🔧
💭
Prevents conditionals where the type is always truthy or always falsy
🔧
💭
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 the use of require statements except in import statements
✔️
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
🔧
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
✔️
🔧
💭
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
💭
Sets preference level for triple slash directives versus ES6-style import declarations
✔️
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
✔️
🔧
💭
Require or disallow spacing between function identifiers and their invocations
🔧
Require or disallow an empty line between class members
🔧
Enforces consistent spacing before function parenthesis
🔧
Contributing
Last updated
Was this helpful?