Sub topic 9
Suppressing a Rule with a Reason
Assume you already know basic TypeScript, have an eslint.config.mjs file, and run ESLint from the command line. This chapter shows how to suppress a rule only when the reported code is understood and justified.
Why suppressions need a reason
ESLint reports code that may be unsafe, confusing, or inconsistent with the project. Sometimes the rule is correct, but the code has a valid exception. For example, a legacy API may return an untyped value that your code must accept at a boundary.
A suppression is an inline comment that tells ESLint not to report a specific rule violation. A good suppression has three properties:
- It targets the smallest possible area.
- It names the rule instead of disabling every rule.
- It records why the exception is safe.
The reason helps reviewers understand the decision and helps future maintainers remove the suppression when the underlying constraint disappears.
flowchart TD
A[ESLint reports a violation] --> B{Is the rule report valid?}
B -->|No| C[Fix configuration or report a bug]
B -->|Yes| D{Can the code be changed safely?}
D -->|Yes| E[Change the code]
D -->|No| F[Add a narrow suppression with a reason]
F --> G[Review the exception later]Use the narrowest directive
eslint-disable-next-line suppresses a rule on the next line only. Put the rule name after the directive and place the reason after --.
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- legacy payload has no known shape
function parseLegacy(input: any): string {
return String(input);
}This directive does not suppress other rules on the function. It also does not suppress the same rule elsewhere in the file.
Use eslint-disable-line when the violation and the directive belong on the same line:
const legacyValue: any = readLegacyValue(); // eslint-disable-line @typescript-eslint/no-explicit-any -- external SDK has no type declarationsThe directive can disable more than one named rule, but keep the list short and specific:
// eslint-disable-next-line no-console, @typescript-eslint/no-unsafe-call -- startup diagnostics run before the logger is available
console.log(initializeApplication());Avoid broad disabled regions
A block directive applies to every matching line until it is closed. It is sometimes useful for generated code, but it can hide unrelated violations.
/* eslint-disable @typescript-eslint/no-explicit-any -- generated adapter mirrors an untyped vendor schema */
export function adaptVendorRecord(record: any): any {
return record;
}
/* eslint-enable @typescript-eslint/no-explicit-any */Close the region immediately after the exception. Do not disable ESLint for an entire file unless the file is generated or has a documented, project-wide reason.
Many teams enable reportUnusedDisableDirectives. This setting makes ESLint report a suppression that no longer hides a violation. Unused suppressions often mean that code or configuration changed and the comment can be removed.
Check the suppression during review
Before adding a directive, ask:
- Is the reported rule enabled for a useful reason?
- Can a type, validation step, or small refactor remove the violation?
- Does the suppression name exactly the rule that reports the problem?
- Does the reason explain the external constraint or safety argument?
- Is the directive close enough to the exceptional line?
A reason such as “needed” does not provide useful context. Prefer a reason that identifies the boundary, limitation, or temporary migration.
Exercise
Exercise
Write an inline directive for a legacy any value returned by an external SDK. The directive must affect only the next line and state why the exception exists.
Show answer
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- external SDK has no type declarations