Loop through a TypeScript enum

Discover how to iterate over the keys and values of a TypeScript enum

Spiral staircase viewed from above, symbolizing iteration through enum keys and values

Enums are awesome. Let’s learn about a cool trick today.

Enums are cool, but string enums are even better. I’m using those quite a lot in my projects because they allow to write clear & readable code.

In this article, I’ll show you how to iterate over the keys & values of a string enum in TypeScript.

Example

Let’s assume that we have the following string enum:

This is a basic enum that represents the possible states of some action (welcome back to the Todo app wonderland!).

Iterating over the values

In order to iterate over the values of this enum, we can use the Object.values() built-in function, which returns an array whose elements are the enumerable property values found on the object.

Note that, as stated in the MDN documentation, the order is the same as that given by looping over the property values of the object manually.

Here I’ve used the functional style, but you can also use a for..of loop, which is more efficient:

With both, you should get the following:

console.log src/database/services/database.service.spec.ts:207
To do

console.log src/database/services/database.service.spec.ts:207
In progress

console.log src/database/services/database.service.spec.ts:207
Done

Neat!

String enums are great because they are easy to refactor and easily “findable” in the code base. This is why I usually prefer string enums over string unions. Although, as pointed out by Frédéric Camblor, it all depends on what you need to be able to do; if you want to iterate over the values at runtime then prefer enums; if you want to iterate over the values at compile time only, then you can use unions strings with mapped types.

Iterating over the keys

Of course, it’s also possible to loop over the keys of a string enum, using Object.keys().

References

Here are a few references about this:

Conclusion

In this small article, I’ve shown you how to easily loop over keys and values of your string enums in TypeScript.

As I’ve explained in my book, string enums are really useful for many use cases. I wish there were as powerful as Java/Kotlin enums, but maybe that’ll come later… ;-)

That's it for today! ✨


Go Further

How to mock TypeScript method overloads with Jest
Learn how to mock TypeScript method overloads with Jest
How to implement input validation with NestJS
Learn how to validate incoming data with NestJS
Introduction to TypeScript
What is TypeScript? Let's find out!

Want to go deeper?

Knowii Community - Master Knowledge Management + AI | From €4.99/month
Join 400+ members mastering Knowledge Management AND AI. Community + Courses + Tools integrated. €500+ value in Knowledge Master tier.

About Sébastien

I'm Sébastien Dubois, and I'm on a mission to help knowledge workers escape information overload. After 20+ years in IT and seeing too many brilliant minds drowning in digital chaos, I've decided to help people build systems that actually work. Through the Knowii Community, my courses, products & services and my Website/Newsletter, I share practical and battle-tested systems.

I write about Knowledge Work, Personal Knowledge Management, Note-taking, Lifelong Learning, Personal Organization, Productivity, and more. I also craft lovely digital products and tools.

If you want to follow my work, then become a member and join our community.

Ready to get to the next level?

Want to level up as a developer?

Found this valuable? Share it with someone who needs it.

Join 6,000+ readers. Get practical systems for knowledge & AI. Free.

Subscribe ✨

Free: Knowledge System Checklist

A clear roadmap to building your own knowledge system. Subscribe and get it straight to your inbox.

6,000+ readers. No spam. Unsubscribe anytime.