SonarJS icon indicating copy to clipboard operation
SonarJS copied to clipboard

FP S3403: in JavaScript type of the initializer is considered

Open vilchik-elena opened this issue 3 years ago • 1 comments

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).

vilchik-elena avatar Jan 14 '22 10:01 vilchik-elena

Reported in https://community.sonarsource.com/t/rule-falsely-claims-dissimilar-types-when-they-might-match/56330

vilchik-elena avatar Jan 14 '22 11:01 vilchik-elena

Sames as https://github.com/SonarSource/SonarJS/issues/2819

ilia-kebets-sonarsource avatar Aug 15 '23 14:08 ilia-kebets-sonarsource

Closing since this is a duplicate.