Advertisement
Advanced TypeScript, Enums

Why do many style guides recommend using a union of string literal types instead of an enum in modern TypeScript?

Short answer

Union literal types (`type Status = 'active' | 'inactive'`) require zero runtime code (fully erased, unlike regular enums which generate a real object), work naturally with plain string values from JSON/APIs without any conversion, and integrate more smoothly with generic type…

Save TypeScript mock interview
Advertisement
AModel answer
Union literal types (`type Status = 'active' | 'inactive'`) require zero runtime code (fully erased, unlike regular enums which generate a real object), work naturally with plain string values from JSON/APIs without any conversion, and integrate more smoothly with generic type inference — enums add real runtime overhead and some quirky edge-case behaviors that literal unions avoid entirely.
// Enum: generates a runtime object, less flexible with raw string values
enum Status { Active = 'ACTIVE', Inactive = 'INACTIVE' }

// Literal union: zero runtime cost, works directly with API string values
type Status = 'ACTIVE' | 'INACTIVE';
Real-world example
Choosing a literal union over an enum specifically because API responses already send raw string values that need no conversion.
Likely follow-ups
  • What ergonomic advantages does an actual enum still retain over a literal union, like autocomplete-friendly namespacing?
Related topics