Skip to content

Bad error message for enum initializers #24210

Closed
@orzFly

Description

@orzFly

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions