Skip to content

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.

declare const label: string;
if (label) {
console.log(label);
}
const settings = { theme: "dark" };
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.

This rule has no options.

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.