- What language is this for?
- Typescript
- Which rule?
- typescript:S6606 - Nullish coalescing should be preferred
- Why do you believe it’s a false-positive/false-negative?
- Because
||
and??
are not interchangeable in this scenario
- Because
- Are you using
- SonarCloud?
- No
- SonarQube - which version?
- No
- SonarLint - which IDE/version?
- IntelliJ, plugin version 10.11.1.79663
- in connected mode with SonarQube or SonarCloud?
- No
- SonarCloud?
- How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
Take this code as an example:
const result = getSomeString()?.trim() || 'default value';
In this case, result
will have 'default value'
whenever getSomeString()?.trim()
returns null
, undefined
or ''
.
If I use ??
here, it will only use the default value if it returns null
or undefined
.
Edit: a minimal test case:
const test = '' || 'test'; // test will have 'test'
const anotherTest = '' ?? 'another test'; // anotherTest will have ''