Enums in TypeScript: Mastering Iteration Techniques
Enums are a fundamental concept in modern programming, allowing us to model categories of things, such as traffic light states, days of the week, or months of the year. One of the significant advantages of enums is that they enable us to map short lists of values into numbers, making it easier to compare and work with them. Over the years, enums have evolved from simple mapping to complex data structures similar to classes, with methods and parameters.
Why Iterate Through Enums in TypeScript?
TypeScript enums are essentially simple objects. For instance, in the definition enum TrafficLight { Green, Yellow, Red }
, Green
is mapped to the number 1, Yellow
to 2, and Red
to 3. If we didn’t specify the mapping Green = 1
, TypeScript would pick 0 as a starting index. Sometimes, we want to iterate over a TypeScript enum, particularly when we need to perform some actions for each element of an enumeration.
Using Object Methods to Iterate Over Enums
The simplest way to iterate over an enum in TypeScript is to convert it to an array using the built-in Object.keys()
and Object.values()
methods. The former returns an array containing the keys of the enum object, while the latter returns an array of the enum’s values.
Reverse Mapping in Numeric Enums
Before exploring other options for iterating over enums, let’s discuss reverse mapping in TypeScript’s numeric enums. Reverse mapping is a TypeScript feature that compiles numeric enums to objects with both a name → value property assignment and a value → name property assignment.
Iterating with For Loops
Instead of relying on Object.keys()
and Object.values()
, another approach is to use for loops to iterate over the keys and then use reverse mapping to get the enum values. TypeScript offers three different kinds of for loop statements, including the for..in
and for..of
loops, which can be used to iterate an enum.
Converting an Enum to a Typed Array
One of the benefits of defining an enum is that we provide an object with a set of limited constants. We can explore several options to iterate an enum by converting it to an iterable object using Object.keys()
or Object.values
; however, with these options, we lose the strict typing of our enum to the specified keys alone because Object.keys()
returns an array of strings.
Using Lodash for Enum Iteration in TypeScript
Lodash is a JavaScript library that provides many utility methods for common programming tasks. We can leverage its forIn
method to iterate over an enum in TypeScript.
Conclusion
In this article, we explored multiple techniques to iterate through enums in TypeScript, including built-in object methods, for loops, and third-party libraries like Lodash. By leveraging methods such as Object.keys()
and Object.values()
, we learned how to handle both string and numeric enums. Additionally, we saw how using for..in
and for..of
loops, combined with type filtering, can provide flexibility when iterating over enums.