Runtime Behavior Differences
What you'll learn: How unions and enums differ completely at runtime—unions vanish while enums generate actual JavaScript code.
Unions Are Compile-Time Only
When you write a union type in TypeScript, it's purely a static type-checking feature. Once your code compiles to JavaScript, the union type completely disappears—no trace of it remains in the output. It's like writing notes in pencil that get erased before publishing.
type Status = "pending" | "active" | "complete";
let current: Status = "pending";
This compiles to plain JavaScript:
let current = "pending";
The Status union? Gone. Zero bytes. Zero runtime cost.
Enums Generate JavaScript Objects
Enums, by contrast, produce actual JavaScript code. They're not just type annotations—they're data structures that exist when your program runs:
enum Status {
Pending = "pending",
Active = "active",
Complete = "complete"
}
Compiles to:
var Status;
(function (Status) {
Status["Pending"] = "pending";
Status["Active"] = "active";
Status["Complete"] = "complete";
})(Status || (Status = {}));
That's real code in your bundle. It takes up space. It runs.
Why This Matters
Union types give you type safety with zero runtime footprint—perfect for keeping bundles small. Enums give you a runtime object you can iterate over or debug, but at the cost of added JavaScript.
Think of unions as invisible guardrails that vanish at build time, while enums are physical objects that ship with your code.
Key Takeaway: Unions have zero runtime cost and disappear during compilation, while enums generate JavaScript objects that exist at runtime—choose based on whether you need runtime access to the values.