- Error
- Reported by the type checker
TS2564
Property '{0}' has no initializer and is not definitely assigned in the constructor.
A class property has no initialiser and isn't assigned in the constructor. Initialise it, assign it in the constructor, or make it optional.
What it means
With strictPropertyInitialization (part of strict), every class property whose type doesn't include undefined must get a value: an initialiser, or an assignment on every path of the constructor.
Example
export class Session {
token: string;
}
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,3): error TS2564: Property 'token' has no initializer and is not definitely assigned in the constructor.
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
- Give it an initial value:
token = "". - Assign it in the constructor.
- Make it optional (
token?: string) or includeundefinedin its type if it can be missing.