Skip to content

Latest commit

 

History

History
128 lines (108 loc) · 48 KB

typescript.md

File metadata and controls

128 lines (108 loc) · 48 KB

@code-pushup/eslint-config/typescript config

Config for strict TypeScript projects.

🏗️ Setup

  1. If you haven't already, make sure to install @code-pushup/eslint-config and its required peer dependencies.

  2. Since this plugin requires additional peer dependencies, you have to install them as well:

    npm install -D eslint-plugin-deprecation
  3. Because this config includes rules which require type information, make sure to configure parserOptions.project in your .eslintrc points to your project's tsconfig. For more information, refer to Linting with Type Information (typescript-eslint), or Configuring ESLint with Typescript (Nx) if using Nx monorepo.

    • Example for library in Nx monorepo:

      {
        "extends": ["../../.eslintrc.json"],
        "ignorePatterns": ["!**/*"],
        "overrides": [
          {
            "files": "*.ts",
            "parserOptions": {
              "project": ["libs/shared-utils/tsconfig.*?.json"]
            }
          }
        ]
      }

    Similarly, you may need to configure a tsconfig file for eslint-plugin-import rules (e.g. if using path aliases in .ts files):

    • Install additional import resolver:

      npm i -D eslint-import-resolver-typescript
    • Example .eslintrc.json for Nx monorepo:

      {
        // ...
        "settings": {
          "import/resolver": {
            "typescript": {
              "alwaysTryTypes": true,
              "project": "tsconfig.base.json"
              // or if using RxJS:
              // "project": ["tsconfig.base.json", "node_modules/rxjs/tsconfig.json"]
            }
          }
        }
      }
  4. Add to extends in your .eslintrc file:

    {
      "extends": ["@code-pushup/eslint-config/typescript"]
    }

📏 Rules (295)

248 rules are included from the default config. For brevity, only the 47 additional rules are listed in this document.

🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.
🧪🚫 Disabled for test files.
🧪⚠️ Severity lessened to warning for test files.

🚨 Errors (33)

Plugin Rule Options Autofix Overrides
@typescript-eslint await-thenable
Disallow awaiting a value that is not a Thenable
💡
@typescript-eslint consistent-type-assertions
Enforce consistent usage of type assertions
assertionStyle: as, objectL...
{
  "assertionStyle": "as",
  "objectLiteralTypeAssertions": "never"
}
🔧, 💡 🧪⚠️
@typescript-eslint no-array-delete
Disallow using the delete operator on array values
💡
@typescript-eslint no-base-to-string
Require .toString() to only be called on objects which provide useful information when stringified
@typescript-eslint no-duplicate-type-constituents
Disallow duplicate constituents of union or intersection types
🔧
@typescript-eslint no-dynamic-delete
Disallow using the delete operator on computed key expressions
🔧
@typescript-eslint no-extraneous-class
Disallow classes used as namespaces
@typescript-eslint no-floating-promises
Require Promise-like statements to be handled appropriately
💡
@typescript-eslint no-for-in-array
Disallow iterating over an array with a for-in loop
@typescript-eslint no-implied-eval
Disallow the use of eval()-like methods
@typescript-eslint no-invalid-void-type
Disallow void type outside of generic or return types
@typescript-eslint no-misused-promises
Disallow Promises in places not designed to handle them
@typescript-eslint no-mixed-enums
Disallow enums from having both number and string members
@typescript-eslint no-non-null-asserted-nullish-coalescing
Disallow non-null assertions in the left operand of a nullish coalescing operator
💡
@typescript-eslint no-non-null-assertion
Disallow non-null assertions using the ! postfix operator
💡
@typescript-eslint no-redundant-type-constituents
Disallow members of unions and intersections that do nothing or override type information
@typescript-eslint no-unnecessary-type-assertion
Disallow type assertions that do not change the type of an expression
🔧
@typescript-eslint no-unsafe-argument
Disallow calling a function with a value with type any
🧪🚫
@typescript-eslint no-unsafe-assignment
Disallow assigning a value with type any to variables and properties
🧪🚫
@typescript-eslint no-unsafe-call
Disallow calling a value with type any
@typescript-eslint no-unsafe-member-access
Disallow member access on a value with type any
🧪🚫
@typescript-eslint no-unsafe-return
Disallow returning a value with type any from a function
🧪🚫
@typescript-eslint no-unsafe-unary-minus
Require unary negation to take a number
@typescript-eslint no-useless-constructor
Disallow unnecessary constructors
@typescript-eslint only-throw-error
Disallow throwing non-Error values as exceptions
@typescript-eslint prefer-literal-enum-member
Require all enum members to be literal values
@typescript-eslint require-await
Disallow async functions which do not return promises and have no await expression
🧪⚠️
@typescript-eslint restrict-plus-operands
Require both operands of addition to be the same type and be bigint, number, or string
allowAny: false, allowBoole...
{
  "allowAny": false,
  "allowBoolean": false,
  "allowNullish": false,
  "allowNumberAndString": false,
  "allowRegExp": false
}
@typescript-eslint restrict-template-expressions
Enforce template literal expressions to be of string type
allowBoolean: true, allowNu...
{
  "allowBoolean": true,
  "allowNumber": true
}
🧪🚫
@typescript-eslint unified-signatures
Disallow two overloads that could be unified into one with a union or an optional/rest parameter
@typescript-eslint use-unknown-in-catch-callback-variable
Enforce typing arguments in .catch() callbacks as unknown
🔧, 💡
deprecation deprecation
Do not use deprecated APIs.
functional immutable-data
Enforce treating data as immutable.
ignoreImmediateMutation: tr...
{
  "ignoreImmediateMutation": true,
  "ignoreClasses": true
}
🧪⚠️

⚠️ Warnings (14)

Plugin Rule Options Autofix Overrides
@typescript-eslint naming-convention
Enforce naming conventions for everything across a codebase
selector: variableLike, met...
[
  {
    "selector": [
      "variableLike",
      "method",
      "typeProperty",
      "parameterProperty",
      "classProperty"
    ],
    "format": [
      "camelCase"
    ]
  },
  {
    "selector": "variable",
    "format": [
      "camelCase",
      "UPPER_CASE"
    ]
  },
  {
    "selector": "typeLike",
    "format": [
      "PascalCase"
    ]
  },
  {
    "selector": "enumMember",
    "format": [
      "PascalCase"
    ]
  },
  {
    "selector": "parameter",
    "modifiers": [
      "unused"
    ],
    "format": null,
    "custom": {
      "regex": "^(_+|[a-z][a-zA-Z0-9]*)$",
      "match": true
    }
  },
  {
    "selector": "objectLiteralProperty",
    "modifiers": [
      "requiresQuotes"
    ],
    "format": null
  },
  {
    "selector": [
      "variable",
      "parameter"
    ],
    "modifiers": [
      "destructured"
    ],
    "format": null
  }
]
@typescript-eslint no-confusing-void-expression
Require expressions of type void to appear in statement position
🔧, 💡
@typescript-eslint no-meaningless-void-operator
Disallow the void operator except when used to discard a value
🔧, 💡
@typescript-eslint no-unnecessary-boolean-literal-compare
Disallow unnecessary equality comparisons against boolean literals
🔧
@typescript-eslint no-unnecessary-condition
Disallow conditionals where the type is always truthy or always falsy
🔧
@typescript-eslint no-unnecessary-template-expression
Disallow unnecessary template expressions
🔧
@typescript-eslint no-unnecessary-type-arguments
Disallow type arguments that are equal to the default
🔧
@typescript-eslint prefer-includes
Enforce includes method over indexOf method
🔧
@typescript-eslint prefer-nullish-coalescing
Enforce using the nullish coalescing operator instead of logical assignments or chaining
ignorePrimitives: {"string"...
{
  "ignorePrimitives": {
    "string": true
  }
}
💡
@typescript-eslint prefer-promise-reject-errors
Require using Error objects as Promise rejection reasons
@typescript-eslint prefer-reduce-type-parameter
Enforce using type parameter when calling Array#reduce instead of casting
🔧 🧪🚫
@typescript-eslint prefer-return-this-type
Enforce that this is used when only this type is returned
🔧
functional prefer-property-signatures
Prefer property signatures over method signatures.
functional prefer-tacit
Replaces x => f(x) with just f.
💡