Ambient / computed enum value is (incorrectly) not typed as a number

Versions: SQ 8.3, scanner 4.3.0, TS plugin 2.1, JS plugin 6.2.1
Rule: typescript:S3402

This rule is supposed to prevent using the addition operator with a mix of numbers and strings. I have code like:

declare enum E { X, Y }
const a = 1 + E.X;

The rule is flagging the addition operator with “Operands of ‘+’ operation must either be both strings or both numbers”. I checked this:

function n(num: number) { }
function s(str: string) { }
n(E.X); // OK
s(E.X); // error, Argument of type 'E' is not assignable to parameter of type 'string'

so the compiler clearly knows that E.X is assignable to number, but for some reason this rule does not. (I specified “ambient” and “computed” because that’s where I ran into this case; I haven’t tested it with any other enum types, which could also be a problem.)

Hello @Thw0rted,

Thank you for reporting this issue!
I created https://github.com/SonarSource/SonarJS/issues/2171 to track this.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.