Sub topic 11
Const Enums and Their Trade-offs
This chapter assumes you know basic TypeScript types, enums, functions, object literals, and switch statements. You should also know that TypeScript is compiled into JavaScript before it runs.
What a const enum does
A const enum is an enum whose members are replaced with their values during compilation. Unlike a regular enum, it usually does not produce a JavaScript object.
const enum Direction {
Up,
Down,
Left,
Right,
}
function move(direction: Direction): string {
switch (direction) {
case Direction.Up:
return "Moving up";
case Direction.Down:
return "Moving down";
case Direction.Left:
return "Moving left";
case Direction.Right:
return "Moving right";
}
}
console.log(move(Direction.Left));The TypeScript compiler can replace Direction.Left with 2. The generated JavaScript is similar to this:
function move(direction) {
switch (direction) {
case 0:
return "Moving up";
case 1:
return "Moving down";
case 2:
return "Moving left";
case 3:
return "Moving right";
}
}
console.log(move(2));The comment that TypeScript normally inserts may show the original enum member:
console.log(move(2 /* Direction.Left */));The comment helps developers read the output. It has no runtime effect.
Why use a const enum?
A regular enum is a runtime value. Code can access it after compilation:
enum Status {
Pending = "pending",
Complete = "complete",
}
console.log(Status.Pending);A const enum is mainly a compile-time convenience. It can avoid creating an object and can make small enum references cheaper in the generated JavaScript.
The compiler can also reject attempts to inspect the whole enum:
const enum Status {
Pending = "pending",
Complete = "complete",
}
// Error with normal const-enum compilation:
// Object.values(Status);The main trade-offs
| Choice | Runtime object | Easy to publish in a library | Values inlined |
|---|---|---|---|
| Regular enum | Yes | Usually | No |
| Const enum | No | Risky | Yes |
| `as const` object | Yes | Yes | No |
Const enums have several practical limitations:
- Different tools may compile TypeScript differently.
- Some tools transpile syntax without performing full type checking.
isolatedModulesand similar workflows can make const enums inconvenient or disallowed.- A published library can expose a const enum in its declaration files while its consumers use a different compiler configuration.
- Inlining can make the consumer's compiled code depend on the enum values from the library version used during compilation.
That last issue can create a version mismatch. Suppose a library changes Pending from 0 to 1. An application compiled against the old declaration may still contain 0, while the application loads a newer library whose code expects 1.
When to prefer another design
For application code compiled by one controlled TypeScript build, a const enum can be reasonable when:
- The enum is used frequently.
- The enum never needs runtime reflection.
- The project controls the compiler and bundler configuration.
- The values are stable.
For shared libraries, generated declaration files, or projects using several build tools, prefer a regular enum or a constant object.
A constant object often gives a clearer boundary between runtime values and types:
const Status = {
Pending: "pending",
Complete: "complete",
} as const;
type Status = (typeof Status)[keyof typeof Status];
function isComplete(status: Status): boolean {
return status === Status.Complete;
}
console.log(isComplete(Status.Complete));This pattern provides runtime access through Status and a string-literal union through the derived Status type. It also works well when values cross a JSON, HTTP, or JavaScript boundary.
Compiler settings and safer publication
The preserveConstEnums compiler option changes the output strategy. It keeps the enum object in the generated JavaScript while still allowing references to be inlined. Some projects use this setting internally, then remove const from emitted declaration files before publishing.
This approach requires a carefully maintained build. It does not remove the need to check the tools that consume the package.
Decision rule
Use a const enum for a private, tightly controlled build where compile-time replacement is useful. Use a regular enum when consumers need a runtime object. Use an as const object plus a derived union when you want explicit runtime values, simple interoperability, and flexible tooling.
Exercise
A package exports a const enum and its users compile the package with different TypeScript tools. What safer replacement should the package consider, and why?
Show answer
It should consider a regular enum or an as const object with a derived union type. These choices provide a stable runtime representation and avoid consumers inlining values from a possibly different package version.