-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
Because of the way the codegen works for enums, we really do not emit sensible code when the enum members have numeric names. Consider this innocent looking example (compiler gives no errors here):
enum E {
1, 2, 3
}
console.log([E[1], E[2], E[3]].join(',')); // Prints 2,3,2 instead of 0,1,2
console.log(E[1].toExponential()); // Crash
console.log(E[2].toExponential()); // Crash
console.log(E[3].toExponential()); // Prints 2e+0Codegen:
var E;
(function (E) {
E[E["1"] = 0] = "1";
E[E["2"] = 1] = "2";
E[E["3"] = 2] = "3";
})(E || (E = {}));
console.log([E[1], E[2], E[3]].join(','));
console.log(E[1].toExponential());
console.log(E[2].toExponential());
console.log(E[3].toExponential());So my question is why do we allow numeric names in an enum, if we cannot emit it right. We can explain to users the semantics for an enum, but our codegen for this pretty confined construct doesn’t adhere to those semantics, and it crashes! Also, an enum with numeric member names does not correspond to an existing javascript pattern that we want to support. I don’t see any reason for this to be allowed.
I realize this is a breaking change. Do people have objections to this break?
Metadata
Metadata
Assignees
Labels
FixedA PR has been merged for this issueA PR has been merged for this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript