Enum vs Union Types
π¨βπΌ You've compared both approaches! Here's a summary:
Enums
- β
Explicit naming (
LogLevel.Info) - β Reverse mapping for numeric enums
- β Familiar to Java/C# developers
- β Extra runtime code
- β More verbose syntax
Union Types
- β No runtime overhead
- β Direct string literal usage
- β Simpler syntax
- β Better tree-shaking
- β No reverse mapping
- β Less explicit naming
π¦ Recommendation: Use union types for most cases. Use enums when you need
reverse mapping or when your team prefers them.
// Most TypeScript developers today prefer:
type Status = 'pending' | 'active' | 'done'