Skip to content

Latest commit

 

History

History
206 lines (168 loc) · 25.5 KB

File metadata and controls

206 lines (168 loc) · 25.5 KB

ESLint Plugin TypeScript

Azure Pipelines GitHub license NPM Version NPM Downloads Commitizen friendly

Installation

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

npm i @typescript-eslint/eslint-plugin --save-dev

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:

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint"]
}

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"]
}

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).

NOTE: If you want to use rules which require type information, you will need to specify a path to your tsconfig.json file in the "project" property of "parserOptions". If you do not do this, you will get a runtime error which explains this.

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  },
  "plugins": ["@typescript-eslint"],
  "rules": {
    "@typescript-eslint/restrict-plus-operands": "error"
  }
}

See @typescript-eslint/parser's README.md for more information on the available "parserOptions".

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

Usage with Prettier

Install eslint-config-prettier to disable our code formatting related rules:

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

Note: Make sure you have eslint-config-prettier@4.0.0 or newer.

Usage with Airbnb

Airbnb has two configs, a base one eslint-config-airbnb-base and one that includes rules for React eslint-config-airbnb.

First you'll need to install the config according to the instructions in one of the links above. npx install-peerdeps --dev eslint-config-airbnb or npx install-peerdeps --dev eslint-config-airbnb-base should work if you are using npm 5+.

Then you should add airbnb (or airbnb-base) to your extends section of .eslintrc. You might also want to turn on plugin:@typescript-eslint/recommended as well to enable all of the recommended rules.

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

Note: You can use Airbnb's rules alongside Prettier, see Usage with Prettier

Supported Rules

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

Name Description ✔️ 🔧 💭
@typescript-eslint/adjacent-overload-signatures Require that member overloads be consecutive ✔️
@typescript-eslint/array-type Requires using either T[] or Array<T> for arrays 🔧
@typescript-eslint/await-thenable Disallows awaiting a value that is not a Thenable ✔️ 💭
@typescript-eslint/ban-ts-ignore Bans “// @ts-ignore” comments from being used ✔️
@typescript-eslint/ban-types Bans specific types from being used ✔️ 🔧
@typescript-eslint/camelcase Enforce camelCase naming convention ✔️
@typescript-eslint/class-name-casing Require PascalCased class and interface names ✔️
@typescript-eslint/consistent-type-assertions Enforces consistent usage of type assertions. ✔️
@typescript-eslint/consistent-type-definitions Consistent with type definition either interface or type 🔧
@typescript-eslint/explicit-function-return-type Require explicit return types on functions and class methods ✔️
@typescript-eslint/explicit-member-accessibility Require explicit accessibility modifiers on class properties and methods
@typescript-eslint/func-call-spacing Require or disallow spacing between function identifiers and their invocations 🔧
@typescript-eslint/generic-type-naming Enforces naming of generic type variables
@typescript-eslint/indent Enforce consistent indentation 🔧
@typescript-eslint/interface-name-prefix Require that interface names should or should not prefixed with I ✔️
@typescript-eslint/member-delimiter-style Require a specific member delimiter style for interfaces and type literals ✔️ 🔧
@typescript-eslint/member-naming Enforces naming conventions for class members by visibility
@typescript-eslint/member-ordering Require a consistent member declaration order
@typescript-eslint/no-array-constructor Disallow generic Array constructors ✔️ 🔧
@typescript-eslint/no-empty-function Disallow empty functions ✔️
@typescript-eslint/no-empty-interface Disallow the declaration of empty interfaces ✔️
@typescript-eslint/no-explicit-any Disallow usage of the any type ✔️ 🔧
@typescript-eslint/no-extra-parens Disallow unnecessary parentheses 🔧
@typescript-eslint/no-extraneous-class Forbids the use of classes as namespaces
@typescript-eslint/no-floating-promises Requires Promise-like values to be handled appropriately. 💭
@typescript-eslint/no-for-in-array Disallow iterating over an array with a for-in loop ✔️ 💭
@typescript-eslint/no-inferrable-types Disallows explicit type declarations for variables or parameters initialized to a number, string, or boolean ✔️ 🔧
@typescript-eslint/no-magic-numbers Disallows magic numbers
@typescript-eslint/no-misused-new Enforce valid definition of new and constructor ✔️
@typescript-eslint/no-misused-promises Avoid using promises in places not designed to handle them ✔️ 💭
@typescript-eslint/no-namespace Disallow the use of custom TypeScript modules and namespaces ✔️
@typescript-eslint/no-non-null-assertion Disallows non-null assertions using the ! postfix operator ✔️
@typescript-eslint/no-parameter-properties Disallow the use of parameter properties in class constructors
@typescript-eslint/no-require-imports Disallows invocation of require()
@typescript-eslint/no-this-alias Disallow aliasing this ✔️
@typescript-eslint/no-type-alias Disallow the use of type aliases
@typescript-eslint/no-unnecessary-qualifier Warns when a namespace qualifier is unnecessary 🔧 💭
@typescript-eslint/no-unnecessary-type-arguments Warns if an explicitly specified type argument is the default for that type parameter 🔧 💭
@typescript-eslint/no-unnecessary-type-assertion Warns if a type assertion does not change the type of an expression ✔️ 🔧 💭
@typescript-eslint/no-unused-vars Disallow unused variables ✔️
@typescript-eslint/no-use-before-define Disallow the use of variables before they are defined ✔️
@typescript-eslint/no-useless-constructor Disallow unnecessary constructors
@typescript-eslint/no-var-requires Disallows the use of require statements except in import statements ✔️
@typescript-eslint/prefer-for-of Prefer a ‘for-of’ loop over a standard ‘for’ loop if the index is only used to access the array being iterated
@typescript-eslint/prefer-function-type Use function types instead of interfaces with call signatures 🔧
@typescript-eslint/prefer-includes Enforce includes method over indexOf method ✔️ 🔧 💭
@typescript-eslint/prefer-namespace-keyword Require the use of the namespace keyword instead of the module keyword to declare custom TypeScript modules ✔️ 🔧
@typescript-eslint/prefer-readonly Requires that private members are marked as readonly if they're never modified outside of the constructor 🔧 💭
@typescript-eslint/prefer-regexp-exec Prefer RegExp#exec() over String#match() if no global flag is provided ✔️ 💭
@typescript-eslint/prefer-string-starts-ends-with Enforce the use of String#startsWith and String#endsWith instead of other equivalent methods of checking substrings ✔️ 🔧 💭
@typescript-eslint/promise-function-async Requires any function or method that returns a Promise to be marked async 💭
@typescript-eslint/quotes Enforce the consistent use of either backticks, double, or single quotes 🔧
@typescript-eslint/require-array-sort-compare Enforce giving compare argument to Array#sort 💭
@typescript-eslint/require-await Disallow async functions which have no await expression ✔️ 💭
@typescript-eslint/restrict-plus-operands When adding two variables, operands must both be of type number or of type string 💭
@typescript-eslint/semi Require or disallow semicolons instead of ASI 🔧
@typescript-eslint/strict-boolean-expressions Restricts the types allowed in boolean expressions 💭
@typescript-eslint/triple-slash-reference Sets preference level for triple slash directives versus ES6-style import declarations ✔️
@typescript-eslint/type-annotation-spacing Require consistent spacing around type annotations ✔️ 🔧
@typescript-eslint/typedef Requires type annotations to exist
@typescript-eslint/unbound-method Enforces unbound methods are called with their expected scope ✔️ 💭
@typescript-eslint/unified-signatures Warns for any two overloads that could be unified into one by using a union or an optional/rest parameter