no-unnecessary-condition
Added in v0.4.2Configuration
rslint.config.ts
Rule Details
Disallow conditionals where the type is always truthy or always falsy.
Any expression being used as a condition must be able to evaluate as truthy or falsy in order to be considered necessary. Conversely, any expression that always evaluates to truthy or always evaluates to falsy is considered unnecessary and will be flagged by this rule.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
Differences from upstream
- An
asyncor generator callback passed to an array predicate method such asfilteris judged by the Promise or generator object it returns, because that object is the value the method consumes.flags.filter(async () => false)reportsalwaysTruthyFuncinstead of upstream'salwaysFalsyonfalse, andflags.filter(async flag => flag)reportsalwaysTruthyFuncwhere upstream reports nothing. - When an accessor's getter can return values its setter does not accept, a read of that property uses the getter's declared type instead of the type TypeScript narrows to after an assignment. After
box.value = 1,box.value ?? 0is not reported whenget value(): number | undefinedis paired withset value(v: number), whereas upstream reportsneverNullish. Other narrowing of such a property is not kept either, soif (box.value !== undefined) box.value ?? 0;is not reported. A value already copied into a variable, such asconst saved = box.value, keeps TypeScript's narrowed type.