DEV Community

Discussion on: We don't have to use Enums on TypeScript? 【Discussion】

Collapse
 
thethirdrace profile image
TheThirdRace

I think @peerreynders pretty much nails why enums are useful, but to add a bit...

The bigger JS bundle size argument is a fact, but then again the impact is so minimal it's like arguing an operation is faster than another when both executes over 1M times per second... performance is not affected in the slightest, it's a non-issue.

I've seen you argue in the comments that union types are more readable, I would argue they're perfectly readable, but so are enums. I would be hard pressed to declare a winner based on readability.

The biggest concern for me, and why I mostly never use union type to create a list of values, is how limiting it can be for your code:

  • Not all editor will allow you to rename a union type value by pressing F2 like VS Code. Some editors will force you to do a search and replace, which is very prone to errors because not all instances found must be replaced...
  • When you have a monorepo with 10+ packages, renaming a value in the union type might not be a huge challenge, but it takes a lot more time to push the new packages to PROD. Not to mention the coordination and/or downtime required for everything to be updated before your system is ready to manage the renamed value. I've seen systems with more than 30 packages very intrinsically linked, union types would be a nightmare...
  • You don't always have a monorepo... Sometimes you're dealing with 30 separate repos, renaming a union type value is NOT a viable option

While I understand it would be better that Typescript doesn't affect the transpiled JS, there are specific reasons why enums exists in so many languages. These reasons are very valid to a point where union type should probably not be used as list of values. Don't get me wrong, there are very real usecases for union type, I'm just saying a list of values might not be the best choice.

While the current Typescript implementation is a bit lacking, enums have many advantages that will make your life much easier while maintaining a real system in production. I would also argue that all the downsides are pretty insignificant when looking at the big picture.

Food for thoughts about the bundle size... You never hear anybody complain about classes in JS increasing the bundle size, but this is a fact. A class cannot tree-shake any of its methods and the sheer amount of developers that insist on using classes everywhere instead of functions is astonishing. Classes will increase your bundle size many order of magnitude more than you could ever achieve through enums. And yet there are tons of posts about not using enum and absolutely none about not using classes unless really needed...

Collapse
 
taishi profile image
Taishi

Thanks for bringing up some very useful points @thethirdrace !

You and @peerreynders 's comments have made me almost fall in love with Enums tbh 😳

Collapse
 
peerreynders profile image
peerreynders

i.e. if bundle size is a concern don't use TypeScript at all; instead use JS Doc TS like Preact does.