DEV Community

Discussion on: How Typescript Enums Work

Collapse
 
smpnjn profile image
Johnny Simpson

I mean the core thing about enums is that they give type safety to specific sets of values. I've also mentioned that you can assign strings or functions to their values - are you looking at it from a memory perspective? This article is more of how you can use enums in TypeScript since some conventions differ from other languages, not how they can be memory efficient - and to be honest that isn't really true in Javascript, right, since enums decompile to objects, meaning they don't have any memory benefits like they would have in maybe other languages. Maybe web assembly will help with that though.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Full disclosure I'm a long time Typescript developer and super fan. So not looking at this from any other languages, memory footprint is still important even in a GC language like JavaScript.
After many years I still feel enums don't make much sense to me. I always end up using string literals "foo" | "bar" keyof typeof obj or something like that.

Thread Thread
 
smpnjn profile image
Johnny Simpson

Yeah, both are valid, enums can become especially useful in large projects with many developers where you want to standardise the options developers have at their disposal for colours, etc.