const enum
A const enum is an enum that emits no runtime object. The compiler replaces every use with the member's literal value. It is faster and smaller than a regular enum, and it breaks under any build tool that compiles one file at a time.
Detailed Explanation
Add const to an enum and the output changes completely:
const enum LogLevel { Debug = 0, Error = 3 }
if (level >= LogLevel.Error) { /* ... */ }
// emits: if (level >= 3 /* LogLevel.Error */) { ... }
No object, no property lookup, no bytes in the bundle. The name exists for humans reading source, and the number is what ships. In 2016 that mattered: a bit-flag check inside a render loop paid for a property access on every call, and every kilobyte of enum object shipped over slow connections.
To make inlining possible, const enum accepts only members whose values can be computed at compile time. No function calls, no references to runtime values. It also gives up dynamic access. Because there is no object, you cannot look a member up by variable, and there is no reverse mapping — LogLevel[someNumber] is an error, not undefined.
The deeper problem is architectural. Inlining requires the compiler to read the declaration at every use site. That is fine for tsc, which sees the whole program. It is impossible for a single-file transpiler — Babel, swc, esbuild, or ts-jest in isolated mode — which is handed one file, strips the types, and never resolves the import. Those tools power most modern build pipelines.
TypeScript's isolatedModules flag exists to flag syntax that cannot survive that model, and const enum is its headline case:
declare const enum— an ambient const enum, the kind shipped in a.d.ts— is a hard error underisolatedModules. There is no declaration to emit and no value to import, so a non-tscpipeline would produce a reference to nothing.- A normal
const enumin your own source is allowed, but the compiler stops inlining it and emits an ordinary enum object instead, so cross-file access still works at runtime. You keep every restriction ofconst enumand lose the only benefit.
For library authors the failure is worse than a compile error. Publish a const enum in your public types and a consumer on esbuild gets undefined where a constant should be, with no diagnostic anywhere.
The language is moving away from this. verbatimModuleSyntax, isolatedDeclarations, and erasableSyntaxOnly (TypeScript 5.8) all push toward syntax that maps one-to-one onto JavaScript so that a runtime can execute TypeScript by deleting types. erasableSyntaxOnly rejects enums outright, const or not.
The practical guidance: do not put a const enum in a package's public API, and do not reach for one in application code — a modern minifier already folds as const object property reads into constants, and the measurable win is close to zero. Prefer a union of string literals, or an as const object when you want a named grouping. If you need an enum's ergonomics, a plain enum is the portable choice.
Code Examples
1. What inlining looks like, and what it forbids.
// 1. What inlining looks like, and what it forbids.
const enum HttpStatus {
Ok = 200,
NotFound = 404,
}
function isMissing(code: number): boolean {
return code === HttpStatus.NotFound;
// emits: return code === 404 /* HttpStatus.NotFound */;
}
const key = 404;
HttpStatus[key];
// Error: A const enum member can only be accessed using a string literal.
// There is no runtime object, so there is nothing to index into.2. The pattern that breaks silently for library consumers.
// 2. The pattern that breaks silently for library consumers.
// --- published-lib/index.d.ts ---
declare const enum Mode {
Strict = "strict",
}
// --- your-app/app.ts, compiled with isolatedModules or esbuild ---
// import { Mode } from "published-lib";
// console.log(Mode.Strict);
// Error under tsc: Cannot access ambient const enums when 'isolatedModules' is enabled.
// Under esbuild there is no error at all — the import resolves to nothing at runtime.3. The replacement: a frozen object plus a derived union.
// 3. The replacement: a frozen object plus a derived union.
const HttpStatus = {
Ok: 200,
NotFound: 404,
} as const;
type HttpStatus = (typeof HttpStatus)[keyof typeof HttpStatus]; // 200 | 404
function describe(code: HttpStatus): string {
return code === HttpStatus.NotFound ? "missing" : "fine";
}
// Ordinary values, so all of this works and none of it needs special tooling.
const codes: number[] = Object.values(HttpStatus); // [200, 404]Common Gotchas
Ambient const enums are incompatible with `isolatedModules`
a `declare const enum` in a `.d.ts` has no emitted value, so any pipeline that compiles files independently cannot resolve it. TypeScript reports this as an error because the alternative is a runtime `undefined`.
Never expose a `const enum` from a library
your consumers choose their own build tool. Under Babel, swc, or esbuild the member reference disappears and they get `undefined` with no warning, in their code, blamed on you.
No reverse mapping and no dynamic lookup
numeric `const enum` values cannot be turned back into their names, and you cannot index with a variable. Code that logs an enum name or builds a menu from one has to be rewritten.
`preserveConstEnums` does not restore normal behavior
it emits the enum object *in addition to* inlining the use sites. You get the bundle cost back without regaining dynamic access, which is rarely what anyone wants.