@typescript-eslint/ban-ts-comment
Disallow @ts-ignore and @ts-nocheck, and require a reason after @ts-expect-error
Also called typescript-eslint/ban-ts-comment, typescript/ban-ts-comment
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#
// @ts-ignoreUse @ts-expect-error instead of @ts-ignore, as @ts-ignore will do nothing if the following line is error-free.
const port: number = process.env.PORT;// @ts-expect-errorInclude a description after the @ts-expect-error directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer.
const port: number = process.env.PORT;// @ts-nocheckDo not use @ts-nocheck because it alters compilation errors.
export const port: number = process.env.PORT;// @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": "allow-with-description" }No problems// @ts-ignore: the types of this package are wrong
import legacy from "legacy-package";{ "minimumDescriptionLength": 10 }1 problem// @ts-expect-error: todoInclude a description after the @ts-expect-error directive to explain why the @ts-expect-error is necessary. The description must be 10 characters or longer.
const port: number = process.env.PORT;The regular expression is tested on everything after the directive, including the space or colon:
{ "ts-expect-error": { "descriptionFormat": "^: TS\\d+ because .+$" } }No problems// @ts-expect-error: TS2322 because PORT is always set
const port: number = process.env.PORT;{ "ts-expect-error": { "descriptionFormat": "^: TS\\d+ because .+$" } }1 problem// @ts-expect-error: PORT is always setThe description for the @ts-expect-error directive must match the ^: TS\d+ because .+$ format.
const port: number = process.env.PORT;