- Error
- Reported by the type checker
TS2450
Enum '{0}' used before its declaration.
An enum member is read before the enum declaration has run. Move the enum declaration above the use.
What it means
An enum compiles to an object that is created where the enum is declared. Reading it before that point gets undefined at runtime.
Example
export const initial = Level.Low;
enum Level {
Low,
High,
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(1,24): error TS2450: Enum 'Level' used before its declaration.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Move the enum declaration above the first use.
- Or use a
const enum, whose members are inlined and don't need the object at runtime.