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?
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.
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'.
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!
Nice video! Just for information, you can convert literal enums to literal unions easily with a small trick: export function initiateLogin(mode: `${LoginMode}`) { // ... }
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.
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.
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 🌹
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.
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
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 RUclips algorithm. I’m here to teach and make the world a better place, thinking sometimes about my own sustainability 😅
@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.
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?
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.
Extremely helpful, slick editing and straight to the point, thanks so much for sharing your knowledge!
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'.
Using only string enums from the very first day and I do not regret it at all. Easy to use and great for readability.
Someone needs to tell all these TS pros to try actually coding professionally and review PRs all day.
I don’t know about other pros, but I review PRs all day everyday. In unions I still trust 😂🌹
Yeah, confusing title, string enums are good, typeSafe from core, dont need "typeof" it is like a const but easy to use
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!
Nice video! Just for information, you can convert literal enums to literal unions easily with a small trick:
export function initiateLogin(mode: `${LoginMode}`) {
// ...
}
I cannot think of a better way to present this kind of content. Truly perfect. Thank you.
Great video as usual Basarat! Thank you very much.
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.
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.
@@pastenml thanks, don’t suppose you have an example?
Been waiting for this one! Awesome video 💪
Completely agree. Enums have always been flawed.
What are your thoughts on const enums?
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 🌹
What theme are you using?
Thanks! 🙇
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.
Nope! Your IDE will display the permitted strings at the call site of that function.
Great video, thank you! : )
Another great video sir. Unions are way better than enums
6:40 I had no idea that is possible
excelente... explosión del cerebro
6:41 why "enter to rename " . It is IDE feature ?
on VSCode:
alt + R (windows)
Option + R (mac)
@@Aaron-qy7vb so it is vs code replacing. I don't know that command. Thanks mate
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
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 RUclips algorithm. I’m here to teach and make the world a better place, thinking sometimes about my own sustainability 😅
@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.