TypeScript Enums are Bad // Alternatives to use

  Рет қаралды 7,453

basarat

basarat

Күн бұрын

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
www.booleanart.com/
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 ❤️ 👇
kzbin.info?su...
🌹 Support 🌹
Join: / basaratali
~
/ basarat
github.com/basarat
basarat.com/
~~ Additional Resources ~~
TypeScript for Professionals:
www.udemy.com/course/typescri...

Пікірлер: 30
@TheFerroman
@TheFerroman Жыл бұрын
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?
@sywareinformatique1745
@sywareinformatique1745 3 ай бұрын
Nice video! Just for information, you can convert literal enums to literal unions easily with a small trick: export function initiateLogin(mode: `${LoginMode}`) { // ... }
@ahmadalghali90
@ahmadalghali90 Жыл бұрын
Extremely helpful, slick editing and straight to the point, thanks so much for sharing your knowledge!
@kmylodarkstar2253
@kmylodarkstar2253 Жыл бұрын
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!
@siavoshoon
@siavoshoon Жыл бұрын
I cannot think of a better way to present this kind of content. Truly perfect. Thank you.
@antonioquintero-felizzola5334
@antonioquintero-felizzola5334 Жыл бұрын
Great video as usual Basarat! Thank you very much.
@lsantosdev
@lsantosdev Жыл бұрын
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'.
@thebigfriezy
@thebigfriezy Жыл бұрын
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.
@anuragroy11
@anuragroy11 Жыл бұрын
Using only string enums from the very first day and I do not regret it at all. Easy to use and great for readability.
@cas818028
@cas818028 Жыл бұрын
Someone needs to tell all these TS pros to try actually coding professionally and review PRs all day.
@basarat
@basarat Жыл бұрын
I don’t know about other pros, but I review PRs all day everyday. In unions I still trust 😂🌹
@Rac0p
@Rac0p Жыл бұрын
Yeah, confusing title, string enums are good, typeSafe from core, dont need "typeof" it is like a const but easy to use
@tylerslater
@tylerslater Жыл бұрын
What theme are you using?
@supalarry1009
@supalarry1009 Жыл бұрын
Great video, thank you! : )
@tomldev
@tomldev Жыл бұрын
Been waiting for this one! Awesome video 💪 Completely agree. Enums have always been flawed. What are your thoughts on const enums?
@basarat
@basarat Жыл бұрын
const enums are great for high performance computing like game dev or compiler development. Only use them if your system doesn’t talk to anything else or it’s all locked in stone 😂 Will cover them separately in the future 🌹
@pawekoaczynski4505
@pawekoaczynski4505 Жыл бұрын
6:40 I had no idea that is possible
@gutylabs9058
@gutylabs9058 Жыл бұрын
excelente... explosión del cerebro
@Nevertheless013
@Nevertheless013 Жыл бұрын
Another great video sir. Unions are way better than enums
@leepowelldev
@leepowelldev Жыл бұрын
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.
@pastenml
@pastenml Жыл бұрын
There is though. Typescript enums are convenient initially but a real pain if you need to export them. String enums become incompatible with themselves if you for example import a library in another library. At least if you bundle, Then the library 1 types will not be valid for checking the second library types.
@leepowelldev
@leepowelldev Жыл бұрын
@@pastenml thanks, don’t suppose you have an example?
@ThanHtutZaw3
@ThanHtutZaw3 Жыл бұрын
6:41 why "enter to rename " . It is IDE feature ?
@Aaron-qy7vb
@Aaron-qy7vb Жыл бұрын
on VSCode: alt + R (windows) Option + R (mac)
@ThanHtutZaw3
@ThanHtutZaw3 Жыл бұрын
@@Aaron-qy7vb so it is vs code replacing. I don't know that command. Thanks mate
@vincent4652
@vincent4652 Жыл бұрын
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.
@ConanCoupe
@ConanCoupe Жыл бұрын
Nope! Your IDE will display the permitted strings at the call site of that function.
@cas818028
@cas818028 Жыл бұрын
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
@basarat
@basarat Жыл бұрын
Further food for thought from your working professional friend ❤️ If you have string unions AND string Enums in your code (most likely). How do you make the choice. How do you keep it consistent. Do your colleagues give mixed signals to each other in PR reviews (sometimes asking for Enums other times asking for unions)? I’m not against string enums. But they don’t have a strong enough reason to exist and be taught. Library definitions will be using string unions because they are easier so might as well make your code consistent. Anyways, I’ve known you long enough to still appreciate you 🌹 PS: I’m not a TS fanboy. Otherwise I wouldn’t be spending ages teaching other things I know, even though they don’t do as well on the KZbin algorithm. I’m here to teach and make the world a better place, thinking sometimes about my own sustainability 😅
@pastenml
@pastenml Жыл бұрын
@cas818028 Don't be a hater. Just search for "typescript enum" here and you will see a lot of other typescript experts saying exactly the same thing as basarat, because they are right. Even the TypeScript maintainers often call them a mistake or recommend against them, because they are a foot gun. Keep using them until you shoot yourself in the foot though, and then you will find these videos again. If you're not building a library it's likely you can keep using enums and not get into any issues.
TypeScript Template Literal Types // So much power ☢️
5:18
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 193 М.
Final muy inesperado 🥹
00:48
Juan De Dios Pantoja
Рет қаралды 11 МЛН
1 класс vs 11 класс  (игрушка)
00:30
БЕРТ
Рет қаралды 3,5 МЛН
Smart Sigma Kid #funny #sigma #comedy
00:19
CRAZY GREAPA
Рет қаралды 8 МЛН
TypeScript Wizardry: Recursive Template Literals
14:47
Tech Talks with Simon
Рет қаралды 36 М.
Why use Type and not Interface in TypeScript
14:12
ByteGrad
Рет қаралды 191 М.
How to use TypeScript Enums and why not to, maybe
12:43
Andrew Burgess
Рет қаралды 18 М.
5 Useful Dunder Methods In Python
16:10
Indently
Рет қаралды 51 М.
TypeScript TYPES vs INTERFACES (Key Differences)
7:16
basarat
Рет қаралды 95 М.
All Rust features explained
21:30
Let's Get Rusty
Рет қаралды 283 М.
RUST Enums ARE Better
5:49
ThePrimeagen
Рет қаралды 137 М.
The TSConfig Cheat Sheet
5:36
Matt Pocock
Рет қаралды 32 М.
tRPC, gRPC, GraphQL or REST: when to use what?
10:46
Software Developer Diaries
Рет қаралды 69 М.
Wow so cute 🥰🙀🤪🐣
0:14
Nguyên Ngốc Nghếch
Рет қаралды 31 МЛН
Try this 🤣  #funny #trending
0:14
Dung ABC
Рет қаралды 26 МЛН
ЛАЙФХАК ДЛЯ РОДИТЕЛЕЙ от Nika.Family
0:45
Привет, Я Ника!
Рет қаралды 6 МЛН
Wow so cute 🥰🙀🤪🐣
0:14
Nguyên Ngốc Nghếch
Рет қаралды 31 МЛН
Это прекрасно🤯
0:27
Бутылочка
Рет қаралды 3,6 МЛН
ЧАПИТОСИИИИК🐾🐾🐾
0:14
Chapitosiki
Рет қаралды 16 МЛН