TypeScript Version: 2.0.3
Code
enum X { A = 010 }
console.log(X.A === 010);
Expected behavior:
Prints true – 010 is an octal literal, both inside and outside an enum declaration.
Actual behavior:
Prints false: In the enum declaration, 010 is understood as 10. This is because evalConstant in src/compiler/checker.ts parses the expression with
case SyntaxKind.NumericLiteral:
return +(<NumericLiteral>e).text;
and while +010 is 8, +"010" is 10.
It’s possible that this behavior is already being used by some to zero-pad enum literal values (though padding is probably more useful for hexadecimal literals), and so it might be a good idea to treat this as an undocumented feature instead of a bug, and to “fix” it by documenting this behavior in the language specification (under 9.2 Enum Members, in the _constant enum members_ definition).
This is similar to #10101, but I think it’s a different bug.
TypeScript Version: 2.0.3
Code
Expected behavior:
Prints
true–010is an octal literal, both inside and outside an enum declaration.Actual behavior:
Prints
false: In the enum declaration,010is understood as10. This is becauseevalConstantinsrc/compiler/checker.tsparses the expression withand while
+010is 8,+"010"is 10.It’s possible that this behavior is already being used by some to zero-pad enum literal values (though padding is probably more useful for hexadecimal literals), and so it might be a good idea to treat this as an undocumented feature instead of a bug, and to “fix” it by documenting this behavior in the language specification (under 9.2 Enum Members, in the _constant enum members_ definition).
This is similar to #10101, but I think it’s a different bug.