ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
const enum is a completely erased, compile-time-only enumeration in TypeScript. Unlike standard enums, which generate a reverse-mapped object via an Immediately Invoked Function Expression (IIFE) in the emitted JavaScript, a const enum leaves no runtime footprint. Instead, the TypeScript compiler directly inlines the evaluated enum member values at every call site during the emit phase.
Compilation Behavior
The defining characteristic of aconst enum is its compilation output. The compiler substitutes the property access with the literal value.
TypeScript Source:
Technical Constraints and Characteristics
- No Runtime Object: Because the enum is erased, you cannot iterate over a
const enumusingObject.keys(),Object.values(), orfor...inloops. The identifierHttpStatusCodedoes not exist at runtime. - Constant Expressions Only: Members of a
const enummust be initialized with constant expressions. They cannot be initialized with dynamically computed values (e.g., the result of a function call or a property access from a runtime object).
- Reverse Mapping: Unlike standard numeric enums, numeric
const enumsdo not support reverse mapping (accessing the string key via the numeric value), as the mapping object is never generated.
Compiler Configuration Impacts
The behavior ofconst enum interacts specifically with certain tsconfig.json compiler options and third-party transpilation tools:
preserveConstEnums: If set totrue, the TypeScript compiler will still inline the values at the call sites, but it will also emit the standard enum object into the JavaScript output.isolatedModules: This is strictly a type-checking flag that warns developers about constructs that would be unsafe for single-file transpilers. It does not changetsc’s own compilation process, nor does it preventtscfrom performing full-program analysis and inlining. Exporting a regularconst enumand importing it into another file is perfectly valid underisolatedModules. TypeScript will only flag ambient const enums (export declare const enum) under this setting, as those lack implementations for single-file transpilers to emit.- Single-File Transpilers (Babel, esbuild, SWC): Because modern bundlers and transpilers operate on a strictly per-file basis, they cannot perform the cross-file analysis required to inline values from an imported
const enum. To handle this, these tools intentionally compileconst enumdeclarations into standard enums (emitting the runtime object). This ensures that imported values resolve correctly at runtime and preventsReferenceErrors.
Master TypeScript with Deep Grasping Methodology!Learn More





