Skip to content

Should not allow numeric names on enum members #134

@JsonFreeman

Description

@JsonFreeman

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+0

Codegen:

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

No one assigned

    Labels

    FixedA PR has been merged for this issueHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions