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

Poor error message for computed string enums #25896

Closed
DanielRosenwasser opened this issue Jul 24, 2018 · 14 comments · Fixed by #37790
Closed

Poor error message for computed string enums #25896

DanielRosenwasser opened this issue Jul 24, 2018 · 14 comments · Fixed by #37790
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

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 24, 2018

https://twitter.com/larymagal/status/1021741263358443520

As a more basic example:

declare function bar(): string
enum E {
  foo = bar();
}

Currently we give the error

Type 'string' is not assignable to type 'E'.

However, we should give an error like

Only numeric enums can have computed members, but this expression has type 'string'.
  If you do not need exhaustiveness checks, consider using an object literal instead.
@mhegazy mhegazy 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 Jul 24, 2018
@mhegazy mhegazy added this to the Community milestone Jul 24, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jul 24, 2018

A PR would be appreciated.

@HarshKhatore
Copy link

@mhegazy I can try this?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 24, 2018

go for it.

@HarshKhatore
Copy link

HarshKhatore commented Jul 25, 2018

Thanks, can you give me a direction on this?
I need to modify the Error handler message, so how do I move on to identifying the file? Is there a way to produce the error stack?

Will it be the ~\ts\bulit\local\tsc.js line 4775?

@mhegazy
Copy link
Contributor

mhegazy commented Jul 25, 2018

Error messages are defined in https://github.com/Microsoft/TypeScript/blob/master/src/compiler/diagnosticMessages.json. you will need to create a new error message, build (jake local), then use the new error message in https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts

@amitdey1987
Copy link

@HarshKhatore are you still pursuing this. If not, then I would like to give it a try.

@DavidSotoA
Copy link

Can I try this one?

@DavidSotoA
Copy link

@mhegazy Hi, I try this but I need some help. I create a new error message in diagnosticMessages.json but I don't know wich function in checker I should put the new error message.

@awgaan
Copy link

awgaan commented Oct 3, 2018

Should this error cover this case?:
"Computed_values_are_not_permitted_in_an_enum_with_string_valued_members_2553" : "Computed values are not permitted in an enum with string valued members."

@awgaan
Copy link

awgaan commented Oct 3, 2018

I am trying to figure this out, but man am I starting to feel stupid.
What I know:

  • Culprit is in compiler/checker.ts in the function computeConstantValue() [26417]
  • In the current state the function bar(): string (in the example) falls through the if-tests and a call is made to checkTypeAssignableTo() which returns an error.

How can I find out if member.initializer is a string foo = "hi" or a functon returning a string foo = bar()?

I would love to solve this, but need some help.

@Codextor
Copy link

Sorry but I didn't notice there's already a PR. Wasted effort. 😞

@zhongsp
Copy link
Contributor

zhongsp commented Dec 26, 2019

Hi @DanielRosenwasser , I've one question:

enum Foo {
  A = 5
}

enum Bar {
  B = Foo.A,
}

Is Bar.B a constant enum member or a computed enum member?

@DanielRosenwasser
Copy link
Member Author

I'm not sure, I think constant but you'd need to check the compiler source.

@okmttdhr
Copy link
Contributor

okmttdhr commented Apr 4, 2020

@DanielRosenwasser
Hi, I submitted the PR #37790, could you check it?

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.

10 participants