Skip to content

Add support for iterating over enums #42465

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

Closed
wants to merge 1 commit into from
Closed

Conversation

Timmmm
Copy link

@Timmmm Timmmm commented Jan 23, 2021

I'm fairly confident about the codegen stuff, but I have no idea about the type checker.

I also couldn't make it work with type aliases - it complains that the alias is only a type but you're trying to use it as a value. Edit: Actually I think that makes sense.

Fixes #42457

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 23, 2021
@awerlogus
Copy link

Does it work with https://www.typescriptlang.org/tsconfig#preserveConstEnums disabled?

@Timmmm
Copy link
Author

Timmmm commented Jan 25, 2021

Does it work with https://www.typescriptlang.org/tsconfig#preserveConstEnums disabled?

You mean enabled? In any case it doesn't work with const enums at all. I think that's what you'd expect given what const enums are.

I also couldn't make it work with type aliases

I realised why this is - you can only iterate over objects. It works with enums because enums are objects (I think they're objects and types). It doesn't work with the type alias because those aren't objects. So it turns out you can do a sort of enum alias by copying the object like this:

enum Test {
  Foo,
  Bar,
}

const AlsoTest = Test;

for (const m of Alsotest) { ...

Kind of confusing but I guess it makes sense.

@awerlogus
Copy link

awerlogus commented Jan 25, 2021

You mean enabled?

No. I asked this because if you disable that option, compiler won't generate the object you can iterate on. So, when somebody will try to iterate some enum, it will produce a runtime error. You must ensure that preserveConstEnums is enabled or show an error otherwise.

@Timmmm
Copy link
Author

Timmmm commented Jan 25, 2021

Ah right. If you check the code (it's really short!) you can see where it only enables iteration over RegularEnums precisely for that reason.

I'm fairly confident about the codegen stuff, but not about the type checker. Iterating over `const enum`s is not supported. This includes a simple test case.
@Timmmm
Copy link
Author

Timmmm commented Jan 26, 2021

Ok I think this is actually ready. I improved the code a bit and added a simple test.

I have not updated the test baselines so that review is easier, because a million of them change (every one with an enum).

@Timmmm Timmmm marked this pull request as ready for review January 26, 2021 12:24
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #42457. If you can get it accepted, this PR will have a better chance of being reviewed.

@sandersn
Copy link
Member

sandersn commented Feb 8, 2021

Given that we want to wait to make runtime changes to enum until they reach stage 3 in TC39, I'm going to close this PR. We might be able to re-use parts of it for the standard enums, but the whole PR is likely to be much more complex.

@sandersn sandersn closed this Feb 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Allow iterating through enum variants with for of
4 participants