SonarJS
SonarJS copied to clipboard
FP S3403: in JavaScript type of the initializer is considered
let x = true; // as there is no type in the declaration, the type of initializer is taken for the variable
if (cond) {
x = "foo"; // compilation error should be produced by TypeScript
}
if (x === "foo") { // FP is reported here
doSomething();
}
This issue mainly appears in JS code as TS code would not compile and we usually analyze valid code.
This is a problem in the way TypeScript compiler is working, so it's not clear how to approach this FP. One (not great) approach could be to disable the rule is there are diagnostics (compilation errors).
Reported in https://community.sonarsource.com/t/rule-falsely-claims-dissimilar-types-when-they-might-match/56330
Sames as https://github.com/SonarSource/SonarJS/issues/2819
Closing since this is a duplicate.