Repository navigation
Bad error message for enum initializers #24210
Description
TypeScript Version: typescript@2.9.0-dev.20180516
Search Terms: enum infinity, TS2553, enum undefined
Code 1
enum TestEnum {
"undefined" = undefined,
}Expected behavior: No string valued member at all. Maybe the error message is wrong.
Actual behavior:
xxx.ts:2:17 - error TS2553: Computed values are not permitted in an enum with string valued members.
2 "undefined" = undefined,
~~~~~~~~~
What does the 0 mean?
var TestEnum;
(function (TestEnum) {
TestEnum[TestEnum["undefined"] = 0] = "undefined";
})(TestEnum || (TestEnum = {}));Playground Link: here
Code 2
enum TestEnum {
"undefined" = undefined,
"NaN" = NaN,
"Infinity" = Infinity,
"-Infinity" = -Infinity,
}Expected behavior: Maybe?
var TestEnum;
(function (TestEnum) {
TestEnum[TestEnum["undefined"] = undefined] = "undefined";
TestEnum[TestEnum["NaN"] = NaN] = "NaN";
TestEnum[TestEnum["Infinity"] = Infinity] = "Infinity";
TestEnum[TestEnum["-Infinity"] = -Infinity] = "-Infinity";
})(TestEnum || (TestEnum = {}));Actual behavior: No error at all!
So what's TestEnum.undefined, TestEnum.NaN, TestEnum.Infinity, -TestEnum.Infinity?
var TestEnum;
(function (TestEnum) {
TestEnum[TestEnum["undefined"] = TestEnum.undefined] = "undefined";
TestEnum[TestEnum["NaN"] = TestEnum.NaN] = "NaN";
TestEnum[TestEnum["Infinity"] = TestEnum.Infinity] = "Infinity";
TestEnum[TestEnum["-Infinity"] = -TestEnum.Infinity] = "-Infinity";
})(TestEnum || (TestEnum = {}));Playground Link: here
Related Issues:
Issue #21959: "It is a compile time error for constant enum expressions to be evaluated to NaN or Infinity."
PR #22022: "disallow nan and infinity in enum member" is made for the previous issue
I think this is not duplicated as #21959 and #22022 are talking about making undefined/NaN/Infinity an error in const enum instead of making them generate correct code in non-const enum.
We only want this error for const enums (enums with a const modifier, I mean), not all enums.
Activity
DanielRosenwasser commentedon May 17, 2018
Yeah this is a terrible and broad error message when complex non-numeric expressions are used in initializers and it needs to be fixed.
[-]Misunderstanding TS2553 in enum with a member of undefined/Infinity/NaN[/-][+]Bad error message for enum initializers[/+]orzFly commentedon May 17, 2018
Daniel Rosenwasser (@DanielRosenwasser) Note in the second code snippet generated by the compiler:
TestEnum.undefinedis wrong. It should beundefinedwakeupneooo commentedon Nov 21, 2020
Hi! Its fixed yet?
aminpaks commentedon Dec 13, 2020
What should the fix look like?
I fixed it by making a new error as in "'null' or 'undefined' cannot be an enum value member", is that the behaviour that we would want?
jakebailey commentedon Jul 11, 2023
After #50528 in TS 5.0, this bad error message is no longer present; the only error on the first code sample is:
Which makes sense, given it's self-referencing and the hover shows that.
TS 3.9 fixed the second example case; the correct error now appears.