-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistent behaviour with Object.values and enums with number values #30487
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
Comments
This is well documented and intended behavior. See documentation for reverse mappings in enums. |
Ah alright, thanks, I skimmed over that part of the documentation. I have no doubt that the generated code is intended, however to me this behaviour conflicts with what is also documented ''enums are real objects that exist at runtime". Given the fact that enums in typecript have been around longer than If the intention is for people to be able to use enums as 'just objects' in runtime, then it should function correctly with all methods on the |
I don't see a conflict there. In order for var DirectionNumber = { Down: 0, 0: "Down" }; But then, without any typescript involvement you get: console.log(Object.values(DirectionNumber)); // ["Down", 0] And this is exactly what you'd expect out of any plain javascript object that obeyed |
I understand the logic and reasoning behind the generated code, and why they currently appear in these arrays, but I disagree that this behaviour is desirable. It's possible to skip enumeration of these key/value pairs generated for reverse mapping using |
The codegen for enums was chosen when ES3 targets (which don't have |
TypeScript Version:
Version used:
3.3.3333
I have reproduced this using
typescript/@next
which at the time of writing is3.4.0-dev.20190319
Search Terms:
object.values
object.values enum
enum values
Code
Expected behavior:
Expecting consistent output, logging the enum values correctly.
This expected behaviour is also shown in the provided playground where I compare it with the erroneous behaviour.
Actual behavior:
When
Object.values
gets passed an enum with numbers as values, the resulting array contains both these numbers AND the keys. This seems unintuitive and unintended behaviour.Playground Link:
Playground link
Related Issues:
The text was updated successfully, but these errors were encountered: