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
leta:string|true|undefined;letb:string|boolean|undefined;letc:boolean|undefined;// test 1 here means some of the abc is truthyconsttest1=Boolean(a||b||c);consttest2=Boolean(a)||Boolean(b)||Boolean(c)console.log(test1);
ESLint Config
{"rules":{"@typescript-eslint/prefer-nullish-coalescing":["warn",{"ignoreConditionalTests":true}]}}
tsconfig
{"compilerOptions": {"strictNullChecks":true }}
Expected Result
Eslint should not try to fix the code, as this is a correct usage.
Actual Result
The fix is invalid, as when:
a = ''b = 'something'
The fix resultBoolean((a ?? b) ?? c)
is false, while the snippet is meant to be true.
I understand that I can write the way intest2
, but it will have performance issues that incease code exec steps, and making the output bigger. The using ofBoolean
is that we usually need a clean state type withboolean
to use it in other places (e.g.: Array.filter)
Additional Info
IMO, when settingignoreConditionalTests: true
, theBoolean
constructor and!(/* codes */)
should also be checked. This means users are wanting to have a full truthy check on all of them.
I have encounted 20+ times with similar issues in my code repo, as it's really common to get a state which is "some of the conditions are truthy"