strictPropertyInitialization
(Strict Property Initialization) in Typescript is a compiler flag, which when set to true, forces us to assign an initial value to all class properties. If we fail to do so then the compiler will raise the “Property has no initializer and is not definitely assigned in the constructor.” error. This flag was introduced in Typescript 2.7
Table of Contents
Setting strictPropertyInitialization
There are two ways in which you can enable strict property initialization in TypeScript.
One is to enable the strict option in tsconfig.ts
1 2 3 4 5 6 7 | { "compilerOptions": { "strict": true, /* Enable all strict type-checking options. */ } } |
or set the strictPropertyInitialization
to true
or false
. This will override the "strict"
option.
1 2 3 4 5 6 7 8 | { "compilerOptions": { "<span style="background-color: inherit; font-family: inherit; font-size: inherit;">strictNullChecks</span>":true "strictPropertyInitialization":true } } |
Note that the --strictNullChecks
flag must be set (either directly or indirectly via --strict
) in order for --strictPropertyInitialization
to have any effect.
Property has no initializer and is not definitely assigned in the constructor
When we declare a property without assigning an initial value, the compiler raises the “Property has no initializer and is not definitely assigned in the constructor” error.
1 2 3 4 5 6 7 | class Person { name:string; } //Property 'name' has no initializer and is not definitely assigned in the constructor. |
There are several ways in which you can solve the above problem
Provide an initial value
There are two ways in which you can provide an initial value to the property. One is setting it explicitly when declaring the property
1 2 3 4 5 | class Person { name:string="Jon Snow"; } |
Another method is to use the constructor function to assign a initial value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Person { name:string; constructor(name:string) { this.name=name } } // or (this is same as above code) class Person { constructor(public name:string) { } } |
Make the property optional
You can also make the Property optional.
1 2 3 4 5 | class Person { name?:string; } |
Use a type that has undefined as one of its value
You can assign the type undefined to the property or any type that allows undefined. For example union type of string & undefined.
1 2 3 4 5 6 7 8 9 10 11 | class Person { name:undefined; } //or class Person { name:string|undefined; } |
Use the definite assignment assertion typescript
The definite assignment assertion is a feature that typescript introduced in version 2.7. We can use it to tell the typescript compiler that we will take care of giving the property its initial value. We do that by placing !
after instance property ( also any variable declaration). The compiler will not raise an error even if detects that the class property lacks an initial value.
1 2 3 4 5 | class Person { name!:string; } |
Set strictPropertyInitialization to false.
You can also set strictPropertyInitialization
to false in the tsconfg.ts. If possible avoid using this option.
1 2 3 4 5 6 7 | { "compilerOptions": { "strictPropertyInitialization":false } } |