Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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 havesearched for related issues and found none that matched my issue.
- I haveread the FAQ and my problem is not listed.
Playground Link
Repro Code
functiontest(a?:boolean):boolean{returna!==false;}test();
ESLint Config
{"rules":{"@typescript-eslint/no-unnecessary-boolean-literal-compare":"error"}}
tsconfig
{"compilerOptions": {"strict":false }}Actual Result
With the configuration above (withoutstrictNullChecks), the@typescript-eslint/no-unnecessary-boolean-literal-compare rule emits a (false) error. Applying the automatic fix translates
functiontest(a?:boolean):boolean{returna!==false;}
into
functiontest(a?:boolean):boolean{returna;}
which is obviously not equivalent asa is nullable.
Root cause seems to be that withoutstrictNullChecks, TypeScript reports the type ofa asboolean instead ofboolean | undefined, making typescript-eslint think thata is not nullable ⇒a !== false is equivalent to justa.
My guess is that theno-unnecessary-boolean-literal-compare rule absolutely has to check forstrictNullChecks and refuse to work (like other rules do, such asprefer-nullish-coalescing).
We already broke our app in runtime after applyingeslint --fix without reviewing each and every change. We can't enablestrict /strictNullChecks as it would emit thousands of errors in the legacy codebase.
Expected Result
Either of
a. (best option) no error, like withstrictNullChecks: true (I assume, not technically possible)
b. eslint refuses to work if theno-unnecessary-boolean-literal-compare rule is enabled, butstrictNullChecks aren't