Description
Before You File a Bug Report Please Confirm You Have Done The Following...
- I have tried restarting my IDE and the issue persists.
- I have updated to the latest version of the packages.
- I have searched for related issues and found none that matched my issue.
- I have read the FAQ and my problem is not listed.
Playground Link
Repro Code
function test(a?: boolean): boolean {
return a !== false;
}
test();
ESLint Config
{
"rules": {
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error"
}
}
tsconfig
Actual Result
With the configuration above (without strictNullChecks
), the @typescript-eslint/no-unnecessary-boolean-literal-compare
rule emits a (false) error. Applying the automatic fix translates
function test(a?: boolean): boolean {
return a !== false;
}
into
function test(a?: boolean): boolean {
return a;
}
which is obviously not equivalent as a
is nullable.
Root cause seems to be that without strictNullChecks
, TypeScript reports the type of a
as boolean
instead of boolean | undefined
, making typescript-eslint think that a
is not nullable ⇒ a !== false
is equivalent to just a
.
My guess is that the no-unnecessary-boolean-literal-compare
rule absolutely has to check for strictNullChecks
and refuse to work (like other rules do, such as prefer-nullish-coalescing
).
We already broke our app in runtime after applying eslint --fix
without reviewing each and every change. We can't enable strict
/ strictNullChecks
as it would emit thousands of errors in the legacy codebase.
Expected Result
Either of
a. (best option) no error, like with strictNullChecks: true
(I assume, not technically possible)
b. eslint refuses to work if the no-unnecessary-boolean-literal-compare
rule is enabled, but strictNullChecks
aren't