Back to Blog
TypeScriptEnumsUnionsTypes

TypeScript Enums vs Union Types

Compare TypeScript enums and union types. Learn when to use each and their trade-offs.

B
Bootspring Team
Engineering
June 18, 2020
7 min read

Both enums and union types define a set of allowed values. Here's when to use each.

Basic Enum

Loading code block...

Basic Union Type

Loading code block...

Const Assertions

Loading code block...

Runtime Behavior

Loading code block...

Const Enums

Loading code block...

Type Safety Comparison

Loading code block...

Discriminated Unions

Loading code block...

Bundle Size

Loading code block...

API Design

Loading code block...

Extending and Combining

Loading code block...

Working with Values

Loading code block...

Recommendations

Loading code block...

Best Practices

Union Types: ✓ Prefer for most use cases ✓ Zero runtime overhead ✓ Better tree-shaking ✓ Easy composition Const Objects: ✓ When you need runtime values ✓ For iteration over values ✓ Namespace organization ✓ Tree-shakeable String Enums: ✓ Team familiarity ✓ Legacy codebase compatibility ✓ IDE autocomplete preference ✓ Reverse mapping not needed Avoid: ✗ Numeric enums (type safety issues) ✗ Heterogeneous enums ✗ Computed enum values ✗ Over-engineering simple cases

Conclusion

Prefer union types for most cases—they're simpler, have zero runtime cost, and compose well. Use const objects when you need runtime values and iteration. String enums are acceptable for team preference but avoid numeric enums due to type safety issues.

Share this article

Help spread the word about Bootspring

Related articles