midTypeScript
What is 'String Enums' vs 'Literal Types' for API contract definitions?
Updated May 4, 2026
Short answer
Enums are runtime objects, while literal types are compile-time-only unions of strings.
Deep explanation
Enums in TypeScript are not just types; they are transpiled into actual JavaScript objects. This allows for reverse mapping (in numeric enums) and iteration at runtime. String literal types (e.g., 'READ' | 'WRITE') are strictly for the compiler and are erased during build. For API contracts, literal types are generally preferred because they align perfectly with JSON data structures and don't require the consumer of your code to import a specific Enum object to pass a valid value.
Real-world example
Using string literal unions for React component props to allow developers to pass plain strings while still receiving full IDE autocompletion.
Common mistakes
- Using enums in a library meant for public consumption, which forces users to import the enum instead of just passing a string.
Follow-up questions
- What are const enums?