TypeScript Enums are Bad // Alternatives to use

preview_player
Показать описание
In this lesson we look at all the reasons why TypeScript enums are not recommended and what are the simple alternatives that you can use that offer greater reliability and future proof code ❤️

🚥 Professional Courses

Chapters
0:00 Intro
0:18 Not Standardized Yet
0:45 Simple Enums are easy to break
1:55 Not Type Safe
2:40 Lookups cause issues
3:58 Issues with String Enums
5:52 Simple Recommended Pattern
6:48 Additional JavaScript Patterns
8:10 Outro

🏷 #basarat #TypeScript #JavaScript #Tutorial

👇 ❤️ Subscribe for MOORE ❤️ 👇

🌹 Support 🌹

~

~~ Additional Resources ~~
TypeScript for Professionals:
Рекомендации по теме
Комментарии
Автор

Great video and valid points.
Nevertheless, I think you understand the DRY principle wrong. This is not about "try not to repeat words in code"
It's actually a pros, that simple string can't be passed instead of enum.
I'm a bit confused with "refactoring by typescript", isn't it done by IDE of choice?

TheFerroman
Автор

Using only string enums from the very first day and I do not regret it at all. Easy to use and great for readability.

anuragroy
Автор

I think numeric enums are now being checked in TS, if you try to use numeric enums with any number that's not defined in the enum itself, it doesn't work anymore, at least as of TS 5, I can't send the link here bc youtube blocks me so, try this same example on the playground you'll get: Argument of type '100' is not assignable to parameter of type 'loginmode'.

lsantosdev
Автор

Enums are not bad. If you use them incorrectly, then yes, you have bad enums. Enums can be super useful for codebase maintenance, especially when you are sharing typescript libraries across multiple repos. Third party library enums are particularly useful when you are expected to use their defined values, but are unsure of what is allowed. It can save you a trip of code diving to derive implementation. Definitely a misleading title.

thebigfriezy
Автор

Extremely helpful, slick editing and straight to the point, thanks so much for sharing your knowledge!

ahmadalghali
Автор

Nice video! Just for information, you can convert literal enums to literal unions easily with a small trick:

export function initiateLogin(mode: `${LoginMode}`) {
// ...
}

sywareinformatique
Автор

I think this could be a good approach if you build libs or something to share others. But for a company you could work with enums, they work well with the IDE. Nice video, thanks for sharing!

kmylodarkstar
Автор

I cannot think of a better way to present this kind of content. Truly perfect. Thank you.

siavoshoon
Автор

Great video as usual Basarat! Thank you very much.

antonioquintero-felizzola
Автор

You don’t mention that at 7.20 this has the same non-dry issue as you mention twice for the enum examples. If you end up using that pattern there really isn’t much between it and a string enum.

leepowelldev
Автор

Been waiting for this one! Awesome video 💪

Completely agree. Enums have always been flawed.

What are your thoughts on const enums?

tomldev
Автор

Another great video sir. Unions are way better than enums

Nevertheless
Автор

I feel like using a union of strings and passing in one of those string literals is too close to using "magic values". It makes code less readable at a glance. If another dev tries to work with your code in the future, they will have to go to every definition of every function to figure out which string union each one is actually using.

vincent
Автор

6:41 why "enter to rename " . It is IDE feature ?

ThanHtutZaw
Автор

Bro you just got a unsub. This is total crap and I am so sick of people taking a crap on enums. I don't think you work in team environments or have to read PR's all day. I cant how many times people comments on PR's cause they thought people were passing "strings" around everywhere only to find out later it is a union type. And you only know that because the type is defined in some other file or package outside of the PR. Enums give a CLEAR and CONCISE, DEFINITE Intetion that the value is of a type/constant. All you TS fanboys need a reality check

cas