- Error
- Reported by the type checker
TS2449
Class '{0}' used before its declaration.
A class is used before its class declaration has run. Move the declaration above the use, or defer the use into a function.
What it means
Class declarations behave like let: the name exists in its scope but can't be used until the declaration has been evaluated. Using it earlier, for example with new at the top of a file, throws at runtime.
Example
export const store = new Store();
class Store {}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(1,26): error TS2449: Class 'Store' 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 class declaration above the code that uses it.
- Uses inside functions that run later are allowed; move the use into one if order can't change.