What const enums Are
In this lesson, you'll learn what const enums are and how they differ from regular enums at compile time.
The Core Idea
A const enum is a special variation of an enum that gets completely erased during compilation. Instead of creating a runtime object (like regular enums do), TypeScript replaces every reference to a const enum member with its actual value directly in the JavaScript output.
Think of it like this: regular enums are like a phone book that stays in your house — whenever you need a number, you look it up. A const enum is like memorizing all the numbers and throwing the phone book away. You still know the numbers, but the book itself doesn't exist anymore.
How It Works
You create a const enum by adding the const keyword before enum:
const enum Direction {
Up,
Down,
Left,
Right
}
let move = Direction.Up;
The generated JavaScript looks like this:
let move = 0; // Direction.Up was replaced with 0
Notice that Direction doesn't exist in the output at all! Every usage of Direction.Up became the literal number 0.
With a regular enum, you'd get an actual object in your JavaScript, but const enums eliminate that entirely.
Why This Matters
Const enums produce zero runtime overhead. The enum exists only during type-checking and then disappears, leaving behind just the values themselves. This makes your bundle slightly smaller and can marginally improve performance.
Key Takeaway: A const enum is fully inlined at compile time, replacing each member reference with its literal value and leaving no runtime object in the emitted JavaScript.