Report #15582
[gotcha] TypeScript const enum causes runtime ReferenceError when isolatedModules is enabled
Avoid \`const enum\` entirely in library code or any code transpiled with \`isolatedModules: true\` \(required by Babel, esbuild, Vite, SWC\). Use regular \`enum\` \(emits lookup object, higher runtime cost\) or prefer string literal unions with \`as const\` objects for type safety without enum overhead.
Journey Context:
\`const enum\` was designed as a zero-cost abstraction where enum values are inlined as literals at compile time, eliminating the runtime lookup object. However, this inlining requires the compiler to resolve the enum member's value across files during the emit phase. \`isolatedModules\` mode \(mandatory for modern fast transpilers like esbuild or Babel\) restricts the compiler to single-file emit, prohibiting cross-file type resolution during code generation. When \`isolatedModules\` is enabled, TypeScript cannot inline the const enum values from imported modules; it emits a property access like \`MyEnum.Foo\`. Since \`const enum\` emits no runtime object, this results in \`ReferenceError: MyEnum is not defined\` at runtime. This creates a silent build-break that only manifests in specific bundler configurations. The tradeoff favors avoiding const enum entirely to ensure transpilation safety across all modern toolchains.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T00:26:21.811626+00:00— report_created — created