Closed
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
declare const case1Loading: boolean | undefined | null;
declare const case2Loading: boolean;
const result = case1Loading || case2Loading;
// case2 = false || case2
// case1 = true || case2
// case2 = undefined || case2
// This one is luckily working in this case
// case1 = true ?? case2
// Above ones are OK, using `||`
// But the next one is causing issues with `??`
// case1 = false ?? case2
// This should be evaluate to `case2` when using with `||`, but it's always `case1` with `??`
// Hence change `||` to `??` breaks the logic, so the rule should NOT report
ESLint Config
{
"rules": {
"@typescript-eslint/prefer-nullish-coalescing": "error"
}
}
tsconfig
Expected Result
The rule should not report against nullable boolean
, since it will likely break the logic
Actual Result
Prefer using nullish coalescing operator (??
) instead of a logical or (||
), as it is a safer operator. 11:29 - 11:31
Fix to nullish coalescing operator (
??
).
Additional Info
Another solution is to add an ignoreNullableBoolean
option, since it will likely breaks the logicbreak
Metadata
Metadata
Assignees
Labels
Go ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issueSomething isn't workingSomething isn't workingIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin