-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
let a: string | true | undefined;
let b: string | boolean | undefined;
let c: boolean | undefined;
// test 1 here means some of the abc is truthy
const test1 = Boolean(a || b || c);
const test2 = Boolean(a) || Boolean(b) || Boolean(c)
console.log(test1);ESLint Config
{
"rules": {
"@typescript-eslint/prefer-nullish-coalescing": [
"warn",
{
"ignoreConditionalTests": true
}
]
}
}tsconfig
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 result Boolean((a ?? b) ?? c) is false, while the snippet is meant to be true.
I understand that I can write the way in test2, but it will have performance issues that incease code exec steps, and making the output bigger. The using of Boolean is that we usually need a clean state type with boolean to use it in other places (e.g.: Array.filter)
Additional Info
IMO, when setting ignoreConditionalTests: true, the Boolean 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"
{ "compilerOptions": { "strictNullChecks": true } }