In TypeScript, you can loop through an enum by using the for...in
loop. Here's an example:
index.ts124 chars10 lines
Note that the for...in
loop also iterates over any properties added to the enum object (e.g. Color["0"] === "Red"
). To avoid this, you can use a type guard to check if the property is a number:
index.ts166 chars12 lines
Alternatively, you can loop through the values of an enum using the Object.values()
method:
index.ts174 chars11 lines
gistlibby LogSnag