2020-03-17
1611
#typescript
Aaron Powell
15368
Mar 17, 2020 ⋅ 5 min read

Why TypeScript enums suck

Aaron Powell I’m a Developer Advocate at Microsoft. My area of specialty is frontend web development focusing on architecture around SPA and other UI-heavy web applications.

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 ⋅ 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 ⋅ 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 ⋅ 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 ⋅ 5 min read
View all posts

2 Replies to "Why TypeScript enums suck"

  1. I’d prefer them over enums every single time. They cover all the regular cases for enums, and the ones they don’t cover, there you shouldn’t use enums either. I’m talking about doing math with enums.
    Wether it’s bit-flags or stuff like `if(day < DaysOfWeek.Saturday)…`.

    Additionally they are nice and readable when I have to deal with JSON or a Database. When I'm greeted by the day "Monday", "Tuesday", "Wednesday" instead of 1,2,3.

    And if someone now thinks, counting down the days of the week is trivial, tell me: what's the first value in your enum? 1? Or did you start at 0 so that the values double as indices over an array of localized strings? And what is the first Entry? Sunday or Monday? You wrote Sunday, in my area that's the last day of the week. And all that just with something as "trivial" as days of week. Now imagine a somewhat more abstract collection of options and you tell me which numeric value translates to what readable option in that enum.

Leave a Reply