TypeScript Enum Iteration
Recently I ran into the issue with enums in TypeScript. Iterating over an enum is possible, but it spits out possibly unintended items. Here the for-in, not for-of, loop logs each item in the enum, numbers and strings:
So the solution seems straightforward… check if an item is a string. Now we should get no numbers:
That includes both numbers and strings! The catch? Those numbers we see printed are actually strings. So some check such as typeof suit === 'string'
is not sufficient. The solution, use Number
as a boolean check: