Limitations of const enums
What you'll learn: Why const enum seems magical at first, but comes with important restrictions that can bite you in modular codebases.
While const enum offers performance benefits through inlining, it has two critical limitations that can cause problems in real-world projects.
Cannot Use Computed Members
A const enum only works with constant values known at compile time. You cannot use computed or calculated values:
// ❌ Error: const enum member initializer must be constant expression
const enum Computed {
Start = 0,
Middle = Math.floor(10 / 2), // Not allowed!
End = 100
}
// ✅ Regular enum allows this
enum RegularComputed {
Start = 0,
Middle = Math.floor(10 / 2), // Works fine
End = 100
}
Since const enum values get inlined at compile time, TypeScript needs to know the exact value immediately — no runtime calculations allowed.
Cross-Module Boundary Problems
When you export a const enum from one module and import it in another, TypeScript tries to inline the values. But if the consuming code doesn't have access to your TypeScript source (like when publishing a library), the inlining fails.
Without the --isolatedModules or --preserveConstEnums flags, users of your library will get errors because the enum doesn't exist at runtime — it was erased during your compilation.
Think of it like this: A const enum is like giving someone a recipe where all measurements are already pre-calculated. That's great — until they try to adjust the recipe without seeing your original measurements.
Key Takeaway: const enum cannot use computed values and creates problems when shared across module boundaries unless special compiler flags preserve the runtime object — making it less portable than regular enums or string literal unions.