Allow iterating through enum variants with for of
See original GitHub issueSuggestion
đ Search Terms
enum, iterate, variants
â Viability Checklist
My suggestion meets these guidelines:
- This wouldnât be a breaking change in existing TypeScript/JavaScript code
- This wouldnât change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isnât a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScriptâs Design Goals.
Note that âis this a runtime feature?â doesnât really apply in this case since enums are already a runtime feature (maybe the only one?). This suggestion just modifies the compilation output slightly.
â Suggestion
The Typescript code for an enumâŚ
enum Colour {
Red,
Green,
Blue,
}
âŚgenerates Javascript like this:
var Colour;
(function (Colour) {
Colour[Colour["Red"] = 0] = "Red";
Colour[Colour["Green"] = 1] = "Green";
Colour[Colour["Blue"] = 2] = "Blue";
})(Colour || (Colour = {}));
Unfortunately thereâs no nice way to iterate over the enum values. The prevailing suggestion on these two very highly upvoted StackOverflow questions is to use for in
and then filter out the reverse mapping entries using typeof
. That sucks:
- Itâs very hacky. Probably really buggy.
- The types are wrong - you get
string
ornumber
instead ofColour
. - It canât handle duplicated enum variants, e.g.
enum { Zero = 0, None = 0, ...}
, although Iâm not 100% sure how duplicates should be handled anyway.
I propose that the above enum code should compile to something like this:
var Colour;
(function (Colour) {
Colour[Colour["Red"] = 0] = "Red";
Colour[Colour["Green"] = 1] = "Green";
Colour[Colour["Blue"] = 2] = "Blue";
Colour[Symbol.iterator] = function* () {
yield 0;
yield 1;
yield 2;
};
})(Colour || (Colour = {}));
This would allow you to write:
function paintColour(c: Colour) {...}
for (const c of Colour) {
paintColour(c);
}
đ Motivating Example / Use Cases
Probably the most common use is generating UI from enums (e.g. a colour drop-down), but this is very general purpose. The StackOverflow questions linked above demonstrate how much people want this. Weâve come across this twice already in our code - once for UI and once for collecting data per enum variant, e.g.:
enum Category {
Code,
Data,
Constant,
Variable,
Temporary,
... lots more ...
}
const totals: Map<Category, number> = new Map();
// Add a 0 entry for all entries.
for (const cat of Category) {
totals.set(cat, 0);
}
for (const variable of ...) {
totals.set(totals.get(variable.category) + variable.size);
}
Note there is a related but slightly different bug here: #39627 but itâs more about how for in
does unexpected things than about making for of
work. I donât think it really matters since you should never ever use for in
anyway.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:7 (3 by maintainers)
Top GitHub Comments
This remains uncommitted and weâre not able to take a PR for it at this time.
Thereâs a reasonable workaround for some situations thatâs worth noting:
Foo
gets deduced as"a" | "b" | "c"
. It doesnât give you symbolic names likeFoo.a
and it doesnât really work for number-based enums but when it does work itâs pretty useful.