A rule that enforces proper usage and validation of TypeScript's "@ts-expect-error" comments for easier error management
yarn add -D eslint-plugin-ts-expect-error-validator
Add it to your ESLint configuration:
{
"plugins": [
"ts-expect-error-validator"
],
"rules": {
"ts-expect-error-validator/no-empty-expect-error": "error"
}
}
Configure the rule to use strict or default validation mode:
{
"plugins": [
"ts-expect-error-validator"
],
"rules": {
"ts-expect-error-validator/no-empty-expect-error": [
"error",
{
"validationMode": "strict"
}
]
}
}
Option | Description |
---|---|
validationMode |
Specifies the validation mode to use. Can be either default or strict . In default mode , only the expected error codes are validated. In strict mode, the error code and error message are validated. Default is default . |
This rule enforces the following rules for @ts-expect-error
comments:
- Each
@ts-expect-error
comment should contain at least one TypeScript error code, enclosed in square brackets ( e.g.[TS123]
). - If using the
strict
validation mode, each error code should be followed by an error description, separated by a hyphen (e.g.[TS2532 - Object is possibly 'undefined']
). - The TypeScript error code should start with
TS
.
// @ts-expect-error [TS6133] - ignore the 'myNumber' is declared but its value is never read message
let myNumber: string | undefined;
// You can also ignore more then one error for a line:
// @ts-expect-error [TS2322, TS6133]
const object: { a: number } = { b: 5 };
- There is a package for validation specified signatures.
This library is released under the MIT License.