STOP using Enums | Typescript

preview_player
Показать описание
#programming #webdevelopment #typescript

In this eye-opening video, we dive into the often-overlooked dangers of TypeScript enums and shed light on their potential pitfalls. While enums may appear harmless and convenient at first glance, there is a lurking dark side that can lead to unforeseen complications and code maintenance nightmares.

Join us as we explore the limitations and caveats associated with TypeScript enums. We start by examining the fundamental nature of enums and how they provide a way to define a set of named constants. Enums can be tempting due to their simplicity and intuitive syntax, but their usage requires careful consideration to avoid unexpected consequences.

Throughout the video, we expose the hidden dangers that arise from the rigid nature of TypeScript enums. One of the major issues is that enums are immutable, meaning once defined, they cannot be modified dynamically during runtime. This limitation can be problematic when the need arises to extend or modify the set of enum values based on evolving requirements.

We also discuss the potential for enum value collisions, which can occur when two or more enums have identical values but represent different concepts. This can lead to confusion and subtle bugs that are challenging to debug and fix. Additionally, the lack of type safety within enums can result in unintended assignments, leading to erroneous behavior that may go unnoticed until runtime.

As we delve deeper, we explore the implications of enums on code readability and maintainability. The tight coupling between enums and their usage throughout the codebase can lead to brittle dependencies, making it difficult to refactor or evolve the enum structure without causing cascading changes. This can result in time-consuming efforts to untangle enum-related dependencies, reducing the agility of the codebase.

To wrap up, we provide practical recommendations and alternative approaches to mitigate the dangers of TypeScript enums. We highlight strategies such as using union types, object literals, or mapping objects to achieve greater flexibility, type safety, and maintainability in your TypeScript projects.

If you're a TypeScript developer looking to gain a deeper understanding of the potential pitfalls associated with enums and learn alternative techniques to enhance your code quality, this video is a must-watch. Prepare to have your assumptions challenged and your development practices refined as we unveil the hidden dangers of TypeScript enums.
Рекомендации по теме
Комментарии
Автор

Will you stop using enums after this video?

Computeshorts
Автор

The point of enums, in any language, is to avoid people passing strings around. Problematic for refactoring or long term maintenance in any even moderate sized code bases.

noideaprojects
Автор

What’s wrong with imports? Also, doesn’t passing the string defeat the point of using an enum

willevans
Автор

For me, enums are definitely better than to pass strings around. Refactoring is a lot easier - how do you refactor a code when you pass around plain strings? Lets say, your "warning" log level becomes "warn" for some reason. You go through full codebase and change it by hand where needed / where typescript yells at you? Yeah, no, thanks.

It has some caveats, for sure, but still looks like a lot better option for me.

TheMMMedo
Автор

Why do want to use strings instead of just referring to the enum name for every time you want to access a member?

spaghettiking
Автор

The whole purpose of ENUMS is to prevent passing arbitrary values but only one of the Enum types!

patrickmullot
Автор

why would you use a string when you already have defined a name? isn't this just like 1 step forward two steps back? although the conversion by typescript is weird but what you did in the last example using a simple object could have been used as Levels.DEBUG.

martygusto
Автор

Honestly, i just make my own string litteral type instead, it works better. I do it like this:
type Direction = “Up” | “Down” | “Left” | “Right”;

BuyMyBeard
Автор

that's actually counter-intuitive, thanks for explaining

ciniss
Автор

How about `type Foo = "one"|"two"|"three"`. I like enums, to me it looks like creating problems where they do not exist😂.

EvgenyBobkin
Автор

Rust enums are the best. Typescript enums should learn from that (sum types supremacy)

fabricehategekimana
Автор

Bro is complaining that enums work like they’re supposed to. Not being able to pass in the raw value is the entire purpose.

bobbyraduloff
Автор

First mistake: use typescript to do pretty much anything.

adriankal
Автор

lol enums don't even exist in golang

iwolfman
Автор

title should say "Typescript"

yanisfourel