typescript enum_error ai_generated true

TS2748: Cannot access ambient const enums when 'isolatedModules' is enabled

ID: typescript/ts-enum-const-assertion

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

Const enums are erased at compile time and cannot work with isolatedModules (required by bundlers like Vite, esbuild).

generic

Workarounds

  1. 90% success Use regular enums instead of const enums
    // Instead of: const enum Direction { Up, Down }
    // Use:
    enum Direction { Up, Down }

    Sources: https://www.typescriptlang.org/docs/handbook/enums.html

  2. 88% success Use 'as const' objects instead of enums
    const Direction = {
      Up: 'UP',
      Down: 'DOWN',
    } as const;
    type Direction = typeof Direction[keyof typeof Direction];

    Sources: https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types

Dead Ends

Common approaches that don't work:

  1. Disable isolatedModules to fix the error 80% fail

    Bundlers like esbuild and Vite require isolatedModules; disabling breaks the build tool

Error Chain

Leads to:
Preceded by:
Frequently confused with: