The “Property ‘…’ has no initializer and is not definitely assigned in the constructor” error occurs when TypeScript’s strictPropertyInitialization option is enabled (either directly or by enabling the strict option) and a class property is not initialized or assigned a value in the constructor.
To fix the “Property ‘…’ has no initializer and is not definitely assigned in the constructor” error, you can Initialize the property with a default value, assign a value to the property in the constructor, use the definite assignment assertion (!), or make the property optional.
Solution 1: Initialize the property with a default value
class MyClass {
myProperty: string = "default value";
}
Solution 2: Assign a value to the property in the constructor
class MyClass {
myProperty: string;
constructor() {
this.myProperty = "value assigned in constructor";
}
}
Solution 3: Using the definite assignment assertion (!)
This tells TypeScript that the property will be assigned a value, even though it can’t verify that in the constructor. Use this option cautiously, as it may lead to runtime errors if the property is not assigned a value as expected.
class MyClass {
myProperty!: string;
}
Solution 4: Make the property optional
If the property has no value assigned, you can declare it optional using the ? modifier. This tells TypeScript that the property can be undefined.
class MyClass {
myProperty?: string;
}
Choose the approach that best fits your use case and coding style. Remember that using definite assignment assertions (option 3) or making the property optional (option 4) can lead to less type safety, so use them cautiously.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.