TypeScript
/Intermediate
Enums vs Union Types
Definition
A comparison between using TypeScript Enums and Literal Union Types (`type Role = 'Admin' | 'Guest'`) to manage distinct sets of values.
Explain Like I'm New
Union Types say: 'You must type the word "Admin" here'. Enums say: 'You must import my specific Enum object and use `Role.Admin` here'. In the modern React community, Union Types are vastly more popular because they are simpler and don't add bloat to your compiled JavaScript.
Real World Example
Migrating a codebase away from Enums because the compiled JavaScript output was bloated, replacing them all with simple String Unions.
Common Use Cases
- •Choosing the right standard for your architecture
Interactive Example
Loading Code Playground...
Interview Questions
basic
- Why do many modern TS developers dislike Enums?
intermediate
- What happens to a Union Type during compilation compared to an Enum?
advanced
- How do `const` enums attempt to solve the compilation bloat?