Enums Don't Interop Well
You'll learn: Why TypeScript enums cause friction when serializing to JSON, storing in databases, or crossing API boundaries—and why primitives are safer in those scenarios.
The Core Problem
When you work with APIs, databases, or JSON serialization, you're crossing the boundary from TypeScript's compile-time world into plain JavaScript runtime values. Enums don't behave like primitives in these scenarios.
String enums serialize to JSON just fine (they're strings), but your API consumer doesn't know they came from an enum. Numeric enums serialize as numbers, which can be confusing without context. When data comes back from JSON, a database, or an external API, TypeScript has no way to validate whether the value is actually a valid enum member—it's just a string or number.
Why This Matters
Imagine you send { status: Status.Active } (where Status.Active is "active") to a REST API. The JSON contains "active". When you receive a response with { status: "active" }, TypeScript won't automatically check if "active" is a valid Status member. You'll need manual validation.
With databases, storing enum values is straightforward, but storing the enum name or handling migrations when enum values change becomes error-prone. Third-party systems won't understand your TypeScript enum—they just see strings or numbers.
Primitives and literal union types ("active" | "inactive") interop naturally because they are the serialized format. No translation layer needed.
The Safer Alternative
For data that crosses boundaries, prefer string literal unions or plain string/number constants. They serialize predictably, require no runtime code, and match exactly what external systems expect.
Key Takeaway: Enums require special handling when data leaves TypeScript's type system—primitives and literal unions interoperate seamlessly with JSON, APIs, and databases without extra validation logic.