strictPropertyInitialization
A declared field has to be assigned
Declaring `token: string` says the property is a string. Before this flag, nothing checked that it ever became one.
- Since
- TypeScript 2.7
- strict
- In strict
- In your tsconfig
"strictPropertyInitialization": true- Compiled with
"strict": true
The same snippet both times. Only the option changed.
With it off
"strictPropertyInitialization": falseexport class Session { token: string }
Compiles clean
With it on
"strictPropertyInitialization": trueexport class Session { token: string }
Emits TS2564
Why the compiler bothers
A class body is the one place in TypeScript where a type annotation and an assignment are written apart from each other, and the gap between them is where an `undefined` lives that the type denies. The flag closes it by requiring an initialiser, a constructor assignment, or the definite assignment assertion `token!: string` — which is you taking responsibility rather than the compiler taking your word for it. It needs `strictNullChecks`, because without it the property being `undefined` is not a contradiction in the first place.
Takeaway
`!` is not a fix. It is a note saying you checked and the compiler could not.
Where to go next
22 options

