- Error
- Reported by the type checker
TS2729
Property '{0}' is used before its initialization.
A class field's initialiser reads another field that is initialised later. Reorder the fields, or compute the value in the constructor.
What it means
Class fields are initialised in the order they're written. An initialiser that reads this.x before x's own initialiser has run sees undefined.
Example
class Box {
area = this.side * 2;
side = 2;
}
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,15): error TS2729: Property 'side' is used before its initialization.
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
- Declare the field that's read first.
- Or compute the value in the constructor, or make it a getter so it's read on use.