How const enums Are Inlined
You'll learn: How TypeScript eliminates const enum overhead by replacing all references with literal values in the compiled JavaScript.
What Happens to const enums at Compile Time
When you use a const enum, TypeScript doesn't generate any actual enum object in your compiled JavaScript. Instead, it completely replaces every reference to a const enum member with its literal value. This process is called inlining.
Think of it like a find-and-replace operation. Imagine you write "ADMIN" on sticky notes throughout your code, but when you publish the final version, you literally write the number 1 everywhere those sticky notes were. The sticky notes (the enum) disappear entirely.
The Inlining Process
When TypeScript compiles code that references a const enum, it:
- Looks up the value associated with each member you reference
- Replaces the member access with the literal value
- Removes the enum declaration entirely from the output
This means there's zero runtime cost—no object lookup, no extra code. Your compiled JavaScript contains only the raw values.
Why This Matters
Inlining makes const enums extremely lightweight. You get all the benefits of named constants with autocomplete and type safety during development, but the final JavaScript is as efficient as if you'd written the literal values by hand.
The trade-off? Because the values are baked in at compile time, if you change a const enum value in a library, consumers must recompile their code—the change won't take effect otherwise.
Key Takeaway: TypeScript inlines const enum references by replacing them with literal values at compile time, eliminating all runtime overhead while preserving type safety during development.