Config for strict TypeScript projects.
-
If you haven't already, make sure to install
@code-pushup/eslint-config
and its required peer dependencies. -
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'] } } } } );
-
-
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);
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.
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
await-thenable Disallow awaiting a value that is not a Thenable |
💡 | |||
no-array-delete Disallow using the delete operator on array values |
💡 | |||
no-base-to-string Require .toString() and .toLocaleString() to only be called on objects which provide useful information when stringified |
||||
no-deprecated Disallow using code marked as @deprecated |
||||
no-duplicate-type-constituents Disallow duplicate constituents of union or intersection types |
🔧 | |||
no-dynamic-delete Disallow using the delete operator on computed key expressions |
🔧 | |||
no-extraneous-class Disallow classes used as namespaces |
||||
no-floating-promises Require Promise-like statements to be handled appropriately |
💡 | |||
no-for-in-array Disallow iterating over an array with a for-in loop |
||||
no-implied-eval Disallow the use of eval() -like methods |
||||
no-invalid-void-type Disallow void type outside of generic or return types |
||||
no-misused-promises Disallow Promises in places not designed to handle them |
||||
no-mixed-enums Disallow enums from having both number and string members |
||||
no-non-null-asserted-nullish-coalescing Disallow non-null assertions in the left operand of a nullish coalescing operator |
💡 | |||
no-non-null-assertion Disallow non-null assertions using the ! postfix operator |
💡 | 🧪🚫 | ||
no-redundant-type-constituents Disallow members of unions and intersections that do nothing or override type information |
||||
no-unnecessary-type-assertion Disallow type assertions that do not change the type of an expression |
🔧 | |||
no-unnecessary-type-parameters Disallow type parameters that aren't used multiple times |
💡 | |||
no-unsafe-argument Disallow calling a function with a value with type any |
🧪🚫 | |||
no-unsafe-assignment Disallow assigning a value with type any to variables and properties |
🧪🚫 | |||
no-unsafe-call Disallow calling a value with type any |
||||
no-unsafe-enum-comparison Disallow comparing an enum value with a non-enum value |
💡 | |||
no-unsafe-member-access Disallow member access on a value with type any |
🧪🚫 | |||
no-unsafe-return Disallow returning a value with type any from a function |
🧪🚫 | |||
no-unsafe-unary-minus Require unary negation to take a number |
||||
no-useless-constructor Disallow unnecessary constructors |
💡 | |||
non-nullable-type-assertion-style Enforce non-null assertions over explicit type casts |
🔧 | |||
only-throw-error Disallow throwing non- Error values as exceptions |
||||
prefer-literal-enum-member Require all enum members to be literal values |
||||
restrict-plus-operands Require both operands of addition to be the same type and be bigint , number , or string |
||||
restrict-template-expressions Enforce template literal expressions to be of string type |
allowBoolean: true, allowNu...{
"allowBoolean": true,
"allowNumber": true
} |
🧪🚫 | ||
return-await Enforce consistent awaiting of returned promises |
error-handling-correctness-..."error-handling-correctness-only" |
🔧, 💡 | ||
unbound-method Enforce unbound methods are called with their expected scope |
||||
unified-signatures Disallow two overloads that could be unified into one with a union or an optional/rest parameter |
||||
use-unknown-in-catch-callback-variable Enforce typing arguments in Promise rejection callbacks as unknown |
🔧, 💡 |
Plugin | Rule | Options | Autofix | Overrides |
---|---|---|---|---|
consistent-type-assertions Enforce consistent usage of type assertions |
assertionStyle: as, objectL...{
"assertionStyle": "as",
"objectLiteralTypeAssertions": "allow"
} |
🔧, 💡 | ||
dot-notation Enforce dot notation whenever possible |
allowPrivateClassPropertyAc...{
"allowPrivateClassPropertyAccess": true,
"allowProtectedClassPropertyAccess": true,
"allowIndexSignaturePropertyAccess": true
} |
🔧 | ||
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
}
] |
|||
no-confusing-void-expression Require expressions of type void to appear in statement position |
🔧, 💡 | |||
no-meaningless-void-operator Disallow the void operator except when used to discard a value |
🔧, 💡 | |||
no-unnecessary-boolean-literal-compare Disallow unnecessary equality comparisons against boolean literals |
🔧 | |||
no-unnecessary-condition Disallow conditionals where the type is always truthy or always falsy |
🔧 | |||
no-unnecessary-template-expression Disallow unnecessary template expressions |
🔧 | |||
no-unnecessary-type-arguments Disallow type arguments that are equal to the default |
🔧 | |||
prefer-includes Enforce includes method over indexOf method |
🔧 | |||
prefer-nullish-coalescing Enforce using the nullish coalescing operator instead of logical assignments or chaining |
ignorePrimitives: {"string"...{
"ignorePrimitives": {
"string": true
}
} |
💡 | ||
prefer-optional-chain Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects |
🔧, 💡 | |||
prefer-promise-reject-errors Require using Error objects as Promise rejection reasons |
||||
prefer-reduce-type-parameter Enforce using type parameter when calling Array#reduce instead of casting |
🔧 | 🧪🚫 | ||
prefer-return-this-type Enforce that this is used when only this type is returned |
🔧 | |||
prefer-string-starts-ends-with Enforce using String#startsWith and String#endsWith over other equivalent methods of checking substrings |
🔧 | |||
require-await Disallow async functions which do not return promises and have no await expression |
💡 | |||
immutable-data Enforce treating data as immutable. |
ignoreImmediateMutation: tr...{
"ignoreImmediateMutation": true,
"ignoreClasses": true,
"ignoreAccessorPattern": "module.exports"
} |
|||
prefer-property-signatures Prefer property signatures over method signatures. |
||||
prefer-tacit Replaces x => f(x) with just f . |
💡 |