They are not interchangeable. Maybe some advice in the short info and the Rule Description would be helpful.
“This rule reports when disjunctions (||) and conditionals (?) can be safely replaced with coalescing (??).”
This is not the case.
Example TypeScript:
Because x can be a bool false the operators can not be safely replaced.
const x: null | undefined | boolean | string;
const y = x || '';
In this case a safe replacement is possible
const x: null | undefined | string;
const y = x || '';
In TypeScript the rule can be fitted to just take effect in cases where false is not possible, I don’t see that in JavaScript, maybe this should be a TS-only rule an be deactivated for JS.
Hi, I think in typing, you mixed up the return values for this part of your post.
It should be the below instead.
It would be nice if you modify it so as to minimize confusion for others. Thanks.
So now it’s a couple of years later and nothing has happened. I can’t see why anyone would prefer a empty string for instance when somevariable ?? ‘default’ , while somevariable || ‘default’ works as expected. And those that really want ‘’ then simply variable||‘’ !
I would hope at least check for string variable should be done and not suggest ?? in that case.
So we shouldn’t be far away from cutting a new version of SonarJS, which will then roll onto SonarQube Cloud and SonarQube Server/Community Build (probably in that order)
Sorry it has taken a while, but hopefully this is all resolved soon!