TS2564

error TS2564: Property ‘name’ has no initializer and is not definitely assigned in the constructor.

This error can occur with TypeScript 2.7 in “strict” mode. TypeScript 2.7 introduced a new flag called --strictPropertyInitialization, which tells the compiler to check that each instance property of a class gets initialized in the constructor body, or by a property initializer. See Strict Class Initialization.

Broken Code ❌

1
2
3
class Dog {
private name: string;
}

Fixed Code ✔️

We have to initialize the name member either at its definition or within the constructor.

Solution 1:

1
2
3
class Dog {
private name: string = 'Laika';
}

Solution 2:

1
2
3
4
5
6
7
class Dog {
private name: string;

constructor() {
this.name = 'Laika';
}
}

Solution 3:

1
2
3
class Dog {
constructor(private name: string = 'Laika') {}
}

Video Tutorial