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...
@vivainio TS is too indirect when file size is the primary constraint of a project, and I am completely unwilling to increase file size simply to make something strictly typed. To me, that's not a worthwhile compromise.
17:58 PM - 17 Apr 2020
i.e. if bundle size is a concern don't use TypeScript at all; instead use JS Doc TS like Preact does.
Rich Harris
@rich_harris
Moved some of my smaller libs to JSDoc TS; thoroughly recommend it. Among other things, the resulting code is generally smaller than transpiled code. Building, testing etc all become much less finicky. And .d.ts files are still generated from source code twitter.com/swyx/status/13…
Interesting counter-trend - maintainers of large open source projects like @Sveltejs and @Deno_land are moving *AWAY* from writing their internals in TypeScript
Just at the same time when the wider dev world is falling in love with TS.
Reasons: build times and code complexity. https://t.co/a74GAoYhwp
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think @peerreynders pretty much nails why
enum
s 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:union type
value by pressingF2
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...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 type
s would be a nightmare...union type
value is NOT a viable optionWhile I understand it would be better that Typescript doesn't affect the transpiled JS, there are specific reasons why
enum
s exists in so many languages. These reasons are very valid to a point whereunion type
should probably not be used as list of values. Don't get me wrong, there are very real usecases forunion type
, I'm just saying a list of values might not be the best choice.While the current Typescript implementation is a bit lacking,
enum
s 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
enum
s. And yet there are tons of posts about not usingenum
and absolutely none about not using classes unless really needed...Thanks for bringing up some very useful points @thethirdrace !
You and @peerreynders 's comments have made me almost fall in love with Enums tbh 😳
i.e. if bundle size is a concern don't use TypeScript at all; instead use JS Doc TS like Preact does.