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

String Based Enum - Reverse Mapping Problem #21935

Closed
kannanwisen opened this issue Feb 14, 2018 · 5 comments
Closed

String Based Enum - Reverse Mapping Problem #21935

kannanwisen opened this issue Feb 14, 2018 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@kannanwisen
Copy link

My TypeScript Version is 2.7.1

  1. enum Country{
  2. India = "INR",
    
  3. France = "EUR",
    
  4. USA = "USD",
    
  5. Germany = France
    
  6. }
  7. let vx : Country = Country.India;
  8. console.log("vx is "+vx);
  9. console.log("enum name is "+Country[vx]);

The output are as follows

vx is INR
enum name is undefined

In Line 9, the reverse mapping is not working for string based enum.

Thanks

Kannan Wisen

@j-oliveras
Copy link
Contributor

j-oliveras commented Feb 14, 2018

String enums has no reverse mapping. Is by design.

See last part of entry post of #15486.

@kannanwisen
Copy link
Author

Thanks for your information

@ghost ghost added the Question An issue which isn't directly actionable in code label Feb 14, 2018
@marcelh-gh
Copy link

@RyanCavanaugh Hi! Could you please explain, why the TS team insists on string enums not having reverse mapping functionality? I have found lots of discussions about it but did not find the rationale behind the decision. Thanks a lot! Cheers, Marcel

@RyanCavanaugh
Copy link
Member

If we provided the reverse map automatically (which, by the way, is not necessarily non-ambiguous!), then you'd have no way to distinguish keys from values unless there was a completely separate object created at runtime. It is trivial to write a function that constructs the reverse map if you want it; our goal is to emit as little code as possible so it makes sense to just emit the data you need (to keep code size down) and let you construct the reverse map on an as-needed basis.

@marcelh-gh
Copy link

@RyanCavanaugh That makes sense. Thanks a lot for the explanation! :-)

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants