Enums Generate Runtime Code
You'll learn why enums are unique in TypeScript: they actually produce JavaScript output, unlike most type features.
The TypeScript Disappearing Act
Most TypeScript features you've encountered so far—type annotations, interfaces, type aliases, literal unions—are completely erased during compilation. They exist only at compile-time to help you catch errors. When TypeScript compiles to JavaScript, they vanish without a trace.
Enums are different.
Enums Produce Real JavaScript Objects
When you declare an enum, TypeScript generates an actual JavaScript object that exists at runtime. This object maps names to values and (for numeric enums) values back to names through reverse mapping.
Think of it like this: an interface is a blueprint that disappears once the building is constructed, but an enum is actual furniture that ships with your final product.
// TypeScript
enum Direction {
Up,
Down,
Left,
Right
}
// Compiles to JavaScript (simplified)
var Direction;
(function (Direction) {
Direction[Direction["Up"] = 0] = "Up";
Direction[Direction["Down"] = 1] = "Down";
Direction[Direction["Left"] = 2] = "Left";
Direction[Direction["Right"] = 3] = "Right";
})(Direction || (Direction = {}));
That JavaScript code actually runs and creates an object in memory.
The Bundle Size Impact
Because enums generate runtime code, they increase your final JavaScript bundle size. If you use ten enums, that's ten JavaScript objects in your output. For most applications, this overhead is negligible, but it's worth knowing—especially when alternatives like string literal unions produce zero runtime code.
Key Takeaway: Unlike interfaces and type aliases, enums aren't purely compile-time constructs—they generate actual JavaScript objects that exist at runtime and contribute to your bundle size.