Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad error message for enum initializers #24210

Closed
orzFly opened this issue May 17, 2018 · 5 comments
Closed

Bad error message for enum initializers #24210

orzFly opened this issue May 17, 2018 · 5 comments
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@orzFly
Copy link

orzFly commented May 17, 2018

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.

@DanielRosenwasser
Copy link
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.

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Help Wanted You can do this Good First Issue Well scoped, documented and has the green light Domain: Error Messages The issue relates to error messaging labels May 17, 2018
@mhegazy mhegazy added this to the Community milestone May 17, 2018
@DanielRosenwasser DanielRosenwasser changed the title Misunderstanding TS2553 in enum with a member of undefined/Infinity/NaN Bad error message for enum initializers May 17, 2018
@orzFly
Copy link
Author

orzFly commented May 17, 2018

@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

@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
ggajos pushed a commit to ggajos/TypeScript that referenced this issue Jul 2, 2020
ggajos pushed a commit to ggajos/TypeScript that referenced this issue Jul 2, 2020
@g0rducci
Copy link

Hi! Its fixed yet?

@aminpaks
Copy link
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?

@jakebailey
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Error Messages The issue relates to error messaging Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants