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

Enum Problem #51150

Closed
louiss0 opened this issue Oct 12, 2022 · 2 comments
Closed

Enum Problem #51150

louiss0 opened this issue Oct 12, 2022 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@louiss0
Copy link

louiss0 commented Oct 12, 2022

Bug Report

🔎 Search Terms

🕗 Version & Regression Information

  • This is a crash
  • This changed between versions ______ and _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

Playground link with relevant code

https://www.typescriptlang.org/play?#code/KYOwrgtgBAYg9nKBvAsAKCpqBxBATAGnSygCEBDQ9AX3XQGM4QBnOAG2ADoBzAJzjAAHABQAiAKLhoAdwCWAFwAWUAPIAjAFbB68qAGtgAT2aiAlOnVadnA8eHw4pzgDM4vceXqLhtgLwA+KEYWdi42OG5hJFtqU3M0YNYOHn4hSTxheIYmJK4+AREJKSg5JVVNbV0AN3I2MGATeMtKzhq6hvsEJ1d3T282+oCgnNDOcMikAeBYrISR5Py0kAzZxNHFwslIEoVlZp0oUHleWQazCwrrI5OOh263Dy9hYQBtWwIoKYBdUyHskOS4yi70+tXqMzocwBeVSgnSmXQQA

💻 Code

image

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

🙁 Actual behavior

When you iterate through enum keys you find that the keys are logged then the values of each key are logged as well when using Object.keys(). When you use Object.entries() you find that the keys and values on the original object are printed and then the values are printed as the keys and the keys printed as values. This all happens during the same iteration.

🙂 Expected behavior

I expected the keys and values to be logged when using Object.entries() . When I use Object.keys() i expect the keys to be logged not the values at all

@fatcerberus
Copy link

Working as intended. Number-valued enums contain bidirectional mappings so that, e.g. Foo[Foo.Bad] is "Bad". This is so you can more easily look up enum values in the debugger. If you don't want the reverse mappings you could try a string-valued enum.

@MartinJohns
Copy link
Contributor

This is working as intended. Enums in TypeScript are reverse mapped. You can see this by looking at the generated JavaScript:

var Foo;
(function (Foo) {
    Foo[Foo["Good"] = 0] = "Good";
    Foo[Foo["Bad"] = 1] = "Bad";
})(Foo || (Foo = {}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants