Skip to content

Latest commit

 

History

History
137 lines (117 loc) · 54.9 KB

typescript.md

File metadata and controls

137 lines (117 loc) · 54.9 KB

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

    • Example for library in Nx monorepo:

      import tseslint from 'typescript-eslint';
      import baseConfig from '../../eslint.config.js';
      
      export default tseslint.config(
        ...baseConfig,
        {
          files: ['**/*.ts'],
          ignores: ['**/code-pushup.config.ts'],
          languageOptions: {
            parserOptions: {
              projectService: true,
              tsconfigRootDir: import.meta.dirname
            }
          }
        }
      );

    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 eslint.config.js for Nx monorepo:

      export default tseslint.config(
        // ...
        {
          settings: {
            'import/resolver': {
              typescript: {
                alwaysTryTypes: true,
                project: 'tsconfig.base.json'
                // or if using RxJS:
                // project: ['tsconfig.base.json', 'node_modules/rxjs/tsconfig.json']
              }
            }
          }
        }
      );
  3. Add to your eslint.config.js file:

    import typescript from '@code-pushup/eslint-config/typescript.js';
    import tseslint from 'typescript-eslint';
    
    export default tseslint.config(...typescript);

📏 Rules (345)

290 rules are included from javascript config. For brevity, only the 55 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 (35)

Plugin Rule Options Autofix Overrides
@typescript-eslint await-thenable
Disallow awaiting a value that is not a Thenable
💡
@typescript-eslint no-array-delete
Disallow using the delete operator on array values
💡
@typescript-eslint no-base-to-string
Require .toString() and .toLocaleString() to only be called on objects which provide useful information when stringified
@typescript-eslint no-deprecated
Disallow using code marked as @deprecated
@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-unnecessary-type-parameters
Disallow type parameters that aren't used multiple times
💡
@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-enum-comparison
Disallow comparing an enum value with a non-enum value
💡
@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 non-nullable-type-assertion-style
Enforce non-null assertions over explicit type casts
🔧
@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 restrict-plus-operands
Require both operands of addition to be the same type and be bigint, number, or string
@typescript-eslint restrict-template-expressions
Enforce template literal expressions to be of string type
allowBoolean: true, allowNu...
{
  "allowBoolean": true,
  "allowNumber": true
}
🧪🚫
@typescript-eslint return-await
Enforce consistent awaiting of returned promises
error-handling-correctness-...
"error-handling-correctness-only"
🔧, 💡
@typescript-eslint unbound-method
Enforce unbound methods are called with their expected scope
@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 Promise rejection callbacks as unknown
🔧, 💡

⚠️ Warnings (20)

Plugin Rule Options Autofix Overrides
@typescript-eslint consistent-type-assertions
Enforce consistent usage of type assertions
assertionStyle: as, objectL...
{
  "assertionStyle": "as",
  "objectLiteralTypeAssertions": "allow"
}
🔧, 💡
@typescript-eslint dot-notation
Enforce dot notation whenever possible
allowPrivateClassPropertyAc...
{
  "allowPrivateClassPropertyAccess": true,
  "allowProtectedClassPropertyAccess": true,
  "allowIndexSignaturePropertyAccess": true
}
🔧
@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",
      "PascalCase"
    ]
  },
  {
    "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-optional-chain
Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects
🔧, 💡
@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
🔧
@typescript-eslint prefer-string-starts-ends-with
Enforce using String#startsWith and String#endsWith over other equivalent methods of checking substrings
🔧
@typescript-eslint require-await
Disallow async functions which do not return promises and have no await expression
💡
functional immutable-data
Enforce treating data as immutable.
ignoreImmediateMutation: tr...
{
  "ignoreImmediateMutation": true,
  "ignoreClasses": true,
  "ignoreAccessorPattern": "module.exports"
}
functional prefer-property-signatures
Prefer property signatures over method signatures.
functional prefer-tacit
Replaces x => f(x) with just f.
💡