Sub topic 9
Suppressing a rule with a reason
Sometimes a lint rule is right in general and wrong for one specific line. An inline directive lets you switch a rule off for that line, and a written reason tells the next reader why. This chapter covers the directive forms, how to scope them tightly, how to require reasons, and what to do when a suppression stops being needed.
It assumes you have a working flat config (eslint.config.mjs) and know how rule names and severities work. The previous chapters on rule settings and file-scoped overrides cover those.
When to suppress a rule
A suppression is a deliberate exception. Before you add one, check three alternatives:
- Fix the code. Most reports point at a real problem.
no-floating-promisesin particular is usually right. - Change the rule's options. If a rule is too strict for your codebase in general, adjust it in the config instead of repeating directives.
- Scope the config. If a whole folder, such as tests or generated code, needs different rules, use a file-scoped entry as shown in the earlier chapter.
Suppress inline only when the code is correct, the rule is sound in general, and the exception is local. Typical cases are a legacy payload with no known shape, an interop boundary with an untyped library, and a deliberate use of a pattern the rule normally bans.
The directive comment forms
ESLint reads special comments to control rules. The forms you will use most are below.
| Directive | Effect |
|---|---|
| `// eslint-disable-next-line rule-name` | Turns the rule off for the next line only. |
| `// eslint-disable-line rule-name` | Turns the rule off for the line the comment is on. |
| `/* eslint-disable rule-name */` | Turns the rule off from this point until the end of the file or a matching enable comment. |
| `/* eslint-enable rule-name */` | Turns the rule back on after a block disable. |
The next-line form is the default choice. It cannot leak onto code you did not intend, and it sits directly above the code it affects.
A directive with a reason
Text after a double dash ( -- ) is a description. ESLint ignores it when deciding what to suppress, but people and tools can read it.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- legacy payload has no known shape
function parseLegacy(input: any): string {
return String(input);
}The directive names one rule, @typescript-eslint/no-explicit-any, and the description explains why any is acceptable here. A good reason answers "why is this safe?" or "why can't this be fixed now?". "Needed" and "ignore" do not.
Rule names include the plugin prefix
Rules from typescript-eslint are named @typescript-eslint/<rule>. If you write no-explicit-any without the prefix, ESLint reports that the rule definition could not be found, and nothing is suppressed. Core ESLint rules, such as no-console, have no prefix.
You can list several rules in one directive, separated by commas:
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return -- third-party callback is untyped
const handler = (value: any) => value;If you omit the rule names, the directive silences every rule on that line. Avoid this. It hides unrelated problems that appear later on the same line.
Keeping suppressions narrow
The narrower the suppression, the less it can hide. Compare these two approaches.
/* eslint-disable @typescript-eslint/no-explicit-any */
// ...two hundred lines of code...A block disable at the top of a file switches the rule off for everything below it, including code added months later. The next-line form limits the exception to the line you reviewed.
If several adjacent lines need the same exception, use a paired block and close it:
/* eslint-disable @typescript-eslint/no-explicit-any -- vendor SDK types are incomplete */
export function readA(raw: any): string {
return String(raw.a);
}
export function readB(raw: any): string {
return String(raw.b);
}
/* eslint-enable @typescript-eslint/no-explicit-any */
export function typedRead(raw: unknown): string {
return String(raw);
}The eslint-enable comment restores the rule, so typedRead is linted normally.
Requiring reasons and catching stale directives
Convention is weak. Two ESLint settings make the practice enforceable. Both live under linterOptions in a flat config entry.
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
{
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
rules: {
'@eslint-community/eslint-comments/require-description': 'off',
},
},
);The example above shows only the linterOptions part as supported by core ESLint. The rules entry is a placeholder that you should delete unless you install that plugin, because ESLint fails on an unknown rule name. Here is the version that runs as written:
// eslint.config.mjs
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import { defineConfig } from 'eslint/config';
export default defineConfig(
eslint.configs.recommended,
tseslint.configs.recommended,
{
linterOptions: {
reportUnusedDisableDirectives: 'error',
},
},
);reportUnusedDisableDirectives reports any directive that no longer suppresses anything. Without it, a directive stays in the file after the code it covered was fixed, and it will silently hide a new problem on that line. In ESLint 9, this option defaults to "warn". Setting it to 'error' makes CI fail on stale directives.
Core ESLint does not require the -- description. To enforce it, use the third-party @eslint-community/eslint-plugin-eslint-comments plugin, whose require-description rule checks every directive. This chapter treats that plugin as optional. Without it, enforce reasons in code review.
Diagram: choosing how to handle a report
Type-checked rules and suppression
Directives work the same for type-checked rules. Consider no-floating-promises, which needs type information to know that save returns a promise.
async function save(id: string): Promise<void> {
// persist the record
}
export function run(): void {
save('a'); // @typescript-eslint/no-floating-promises reports this call
}
export async function runFixed(): Promise<void> {
await save('a');
}Awaiting the call is the correct fix. A suppression here would be wrong unless the promise is deliberately left running. If it is, prefer the explicit void operator, which no-floating-promises accepts by default:
async function track(event: string): Promise<void> {
// send analytics
}
export function onClick(): void {
void track('click'); // fire and forget on purpose
}The void operator states intent in the code itself and needs no directive. Check whether a rule has a built-in way to express an exception before you reach for a comment.
The `@ts-expect-error` comparison
TypeScript has its own suppression comments, and they are a separate system. ESLint directives do not silence compiler errors, and @ts-expect-error does not silence lint rules.
| Comment | Silences | Behavior when nothing is wrong |
|---|---|---|
| `// eslint-disable-next-line ...` | ESLint rules | Reported only if `reportUnusedDisableDirectives` is on. |
| `// @ts-expect-error` | TypeScript compiler errors | The compiler reports an error that the comment is unused. |
| `// @ts-ignore` | TypeScript compiler errors | Nothing is reported. |
The rule @typescript-eslint/ban-ts-comment is part of the recommended preset. By default it bans @ts-ignore and @ts-nocheck, and it allows @ts-expect-error only with a description. Prefer @ts-expect-error with a reason, because it fails the build when the underlying error disappears.
// @ts-expect-error -- upstream typing for `legacy` is missing the `id` field
const id: string = legacy.id;Common errors
- Misspelled or unprefixed rule name. ESLint reports a "Definition for rule ... not found" error on the directive. Copy the rule name from the lint report.
- Wrong line.
eslint-disable-next-linecovers only the line directly after the comment. If a formatter wraps the statement across several lines, the report may land on a different line than you expect. Place the comment above the line where the report appears. - Forgotten
eslint-enable. A blockeslint-disablestays active to the end of the file. Close it, or use thenext-lineform. - Disabling every rule. A directive with no rule names hides all reports on the line. Always name the rule.
- Suppressing instead of fixing. If you add many directives for the same rule, the rule is probably misconfigured for your project. Change the config.
Auditing existing suppressions
To see which directives your project relies on, search for them:
npx eslint . --report-unused-disable-directivesThis command reports directives that do nothing, which lets you delete them. A plain text search such as grep -rn "eslint-disable" src lists all remaining directives for review.
Exercise
A teammate adds // eslint-disable-next-line with no rule name above a line that uses any, and a month later a second problem appears on that line without any report. What went wrong, and how would you write the directive instead?
Show answer
A directive with no rule names silences every rule on that line, so the new problem was hidden too. Name the rule and give a reason: // eslint-disable-next-line @typescript-eslint/no-explicit-any -- legacy payload has no known shape. Also set linterOptions.reportUnusedDisableDirectives to 'error' so stale directives are reported.
Exercise
You call track('click') from a click handler and @typescript-eslint/no-floating-promises reports it. You do not want to wait for the result. What is the best way to express this without an ESLint directive?
Show answer
Mark the call as intentionally unawaited with the void operator: void track('click');. The rule accepts this by default, and the code states the intent.