unnecessaryConditions
Reports conditions whose outcomes are statically known.
✅ This rule is included in the ts logicalpresets.
This checker-backed rule uses TypeScript’s flow-narrowed types to find truthiness, nullability, comparisons, switch cases, optional chains, logical assignments, and built-in Array predicate callbacks whose results are known.
It requires strictNullChecks so that the checker can distinguish nullish values from other values.
Examples
Section titled “Examples”declare const label: string;
if (label) { console.log(label);}const settings = { theme: "dark" };const theme = settings?.theme;declare const label: string | undefined;
if (label) { console.log(label);}declare const settings: { theme: string } | undefined;const theme = settings?.theme;Optional-chain diagnostics offer a suggestion that removes only the unnecessary ?. token.
The rule does not apply an unconditional fix because preserving evaluation and control-flow intent can require a larger edit.
Options
Section titled “Options”This rule has no options.
When Not To Use It
Section titled “When Not To Use It”Do not use this rule in projects that cannot enable strictNullChecks or whose generated declarations do not accurately describe runtime nullability.
The rule intentionally does not inspect arbitrary assertion calls or user-defined type-predicate calls, and it does not infer that unrelated object interfaces can never overlap.
When noUncheckedIndexedAccess is disabled, it conservatively retains checks on array elements, dynamic tuple indexes, and index-signature accesses because those reads can produce undefined at runtime.
Rule Equivalents
Section titled “Rule Equivalents”- Biome:
noUnnecessaryConditions - ESLint:
@typescript-eslint/no-unnecessary-condition - Oxlint:
typescript/no-unnecessary-condition
