Skip to content

Bad error message for enum initializers #24210

Closed

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.

Activity

  1. DanielRosenwasser commented on May 17, 2018

    @DanielRosenwasser
    Member

    Yeah this is a terrible and broad error message when complex non-numeric expressions are used in initializers and it needs to be fixed.

  2. added this to the milestone on May 17, 2018
  3. changed the title [-]Misunderstanding TS2553 in enum with a member of undefined/Infinity/NaN[/-] [+]Bad error message for enum initializers[/+] on May 17, 2018
  4. orzFly commented on May 17, 2018

    @orzFly
    Author

    Daniel Rosenwasser (@DanielRosenwasser) Note in the second code snippet generated by the compiler:

    TestEnum[TestEnum["undefined"] = TestEnum.undefined] = "undefined";

    TestEnum.undefined is wrong. It should be undefined

  5. wakeupneooo commented on Nov 21, 2020

    @wakeupneooo

    Hi! Its fixed yet?

  6. aminpaks commented on Dec 13, 2020

    @aminpaks
    Contributor

    Yeah this is a terrible and broad error message when complex non-numeric expressions are used in initializers and it needs to be fixed.

    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?

  7. jakebailey commented on Jul 11, 2023

    @jakebailey
    Member

    After #50528 in TS 5.0, this bad error message is no longer present; the only error on the first code sample is:

    Property '"undefined"' is used before being assigned.(2565)

    image

    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.

  8. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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