TypeScript Reference ยท tsconfig: Type Checking Options
TypeScript strictPropertyInitialization option
Reference for strictPropertyInitialization in TypeScript: why class fields must be assigned in the constructor, and the fixes: defaults, optional, ! and union types.
strictPropertyInitialization requires every class field whose type does not include undefined to be definitely assigned: either with an initializer on the declaration or in the constructor on every path. It stops the classic bug where a field is read before anything gave it a value, which in JavaScript yields undefined while the type claims it is a string.
The flag needs strictNullChecks to be on, and it is part of strict. The error is TS2564: "Property 'name' has no initializer and is not definitely assigned in the constructor."
class User { name: string; // error TS2564 age = 0; // fine: has an initializer constructor() {} }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "strict": true, "strictPropertyInitialization": false } }
| Fix | When to use it |
|---|---|
x = value; |
A sensible default exists. |
| Assign in the constructor | The value comes from constructor parameters. Every branch must assign it. |
x?: T or x: T | undefined |
The field can truly be missing. Callers must then check it. |
x!: T |
Another mechanism assigns it before use, for example a framework, dependency injection or an init() method. |
Parameter properties (constructor(public x: T)) |
Shortest form when the value is a constructor argument. |
Edge cases: assignments inside a method called from the constructor are not tracked, so the compiler still reports the error. Assignments in a nested callback or after an early return in some path are also not counted. Abstract and declared fields are exempt, and fields of type any, unknown or a type including undefined never need initialization.
Usage notes
- Why not always
!? The assertion silences the check completely. Use it only when there is a real guarantee, and consider whether the object can be built in a valid state in the constructor instead. - Constructors that call
init()are better written so thatinitreturns the values and the constructor assigns them, or a static factory method builds the object fully. - Inheritance: a subclass field with an initializer runs after
super()returns, so a base constructor that reads such a field seesundefined. useDefineForClassFields(default with target ES2022 and later) emits field declarations as real class fields, which changes initialization order slightly. Adeclarefield avoids emitting it.- Frameworks that fill fields later (ORMs, Angular
@Input, test setup) are the usual reason for!:. - Optional is honest.
x?: Tdocuments that code must handle a missing value, which is often what really happens.
Related
- strict
- strictNullChecks
- Tutorial: Classes, Strict mode