Closed
Description
TypeScript Version: [email protected], 2.9.2, playround
Search Terms:
== as typeguard
number typeguard
number type coercion
Code
function test(level: number | string):number {
if (level == +level) {
const q2 = level; //number
return level;
}
return 0;
}
alert(test(5) + 1);
alert(test("5") + 1)
Expected behavior:
Compiler error
Actual behavior:
The code compiles. As mentioned in the comment, typescript assumes that level inside the if block is a number; however in reality that condition passes also for number-like string ("5")