Linter · Rules
@typescript-eslint/ban-ts-comment
Disallow @ts-ignore and @ts-nocheck, and require a reason after @ts-expect-error
A @ts- comment turns off type errors. @ts-ignore hides every error on the next line, now and later, and stays there when the error is gone. @ts-nocheck turns off type checking for the whole file.
@ts-expect-error is the safer one, because TypeScript complains when the line no longer has an error. By default it's allowed if a description of at least 3 characters follows it, so the next reader knows why it's there.
This rule is on by default in TypeScript files.
Configure
On by default as a warning. To turn it off:
[lint.rules]
"@typescript-eslint/ban-ts-comment" = "off" # or "error"
Every option, set to its default:
[lint.rules]
"@typescript-eslint/ban-ts-comment" = ["warn", {
# true, false, "allow-with-description" or object
ts-expect-error = "allow-with-description",
ts-ignore = true, # true, false, "allow-with-description" or object
ts-nocheck = true, # true, false, "allow-with-description" or object
ts-check = false, # true, false, "allow-with-description" or object
minimumDescriptionLength = 3,
}]
Examples
Reported:
// @ts-ignore
const port: number = process.env.PORT;
// @ts-expect-error
const port: number = process.env.PORT;
// @ts-nocheck
export const port: number = process.env.PORT;
Not reported:
// @ts-expect-error: PORT is always set in production
const port: number = process.env.PORT;
Options
| Option | Default | What it does |
|---|---|---|
ts-expect-error | "allow-with-description" | How to treat @ts-expect-error. |
ts-ignore | true | How to treat @ts-ignore. |
ts-nocheck | true | How to treat @ts-nocheck. |
ts-check | false | How to treat @ts-check. |
minimumDescriptionLength | 3 | How many characters a description needs. |
Each directive takes one of these values:
| Value | What it does |
|---|---|
true | Report the directive. |
false | Allow it. |
"allow-with-description" | Allow it if a description follows. |
{ "descriptionFormat": "..." } | Allow it if the description matches this regular expression. |
// @ts-ignore: the types of this package are wrong
import legacy from "legacy-package";
// @ts-expect-error: todo
const port: number = process.env.PORT;
The regular expression is tested on everything after the directive, including the space or colon:
// @ts-expect-error: TS2322 because PORT is always set
const port: number = process.env.PORT;
// @ts-expect-error: PORT is always set
const port: number = process.env.PORT;