@typescript-eslint/ban-ts-comment

Disallow @ts-ignore and @ts-nocheck, and require a reason after @ts-expect-error

warn by defaulthas suggestions

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:

bunfig.toml
[lint.rules]
"@typescript-eslint/ban-ts-comment" = "off" # or "error"

Every option, set to its default:

bunfig.toml
[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#

example.ts1 problem
// @ts-ignore
const port: number = process.env.PORT;
example.ts1 problem
// @ts-expect-error
const port: number = process.env.PORT;
example.ts1 problem
// @ts-nocheck
export const port: number = process.env.PORT;
example.tsNo problems
// @ts-expect-error: PORT is always set in production
const port: number = process.env.PORT;

Options#

OptionDefaultWhat it does
ts-expect-error"allow-with-description"How to treat @ts-expect-error.
ts-ignoretrueHow to treat @ts-ignore.
ts-nochecktrueHow to treat @ts-nocheck.
ts-checkfalseHow to treat @ts-check.
minimumDescriptionLength3How many characters a description needs.

Each directive takes one of these values:

ValueWhat it does
trueReport the directive.
falseAllow it.
"allow-with-description"Allow it if a description follows.
{ "descriptionFormat": "..." }Allow it if the description matches this regular expression.
example.tswith { "ts-ignore": "allow-with-description" }No problems
// @ts-ignore: the types of this package are wrong
import legacy from "legacy-package";
example.tswith { "minimumDescriptionLength": 10 }1 problem
// @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:

example.tswith { "ts-expect-error": { "descriptionFormat": "^: TS\\d+ because .+$" } }No problems
// @ts-expect-error: TS2322 because PORT is always set
const port: number = process.env.PORT;
example.tswith { "ts-expect-error": { "descriptionFormat": "^: TS\\d+ because .+$" } }1 problem
// @ts-expect-error: PORT is always set
const port: number = process.env.PORT;