. Advertisement .
..3..
. Advertisement .
..4..
Errors are a part of development. You need to be able to fix them so that you can get back to your work. The error “Property ‘…’ has no initializer and is not definitely assigned in the constructor” is a particularly annoying error to fix. However, by following our tips, you can soon know how to fix this error easily.
How Does The Error “Property ‘…’ has no initializer and is not definitely assigned in the constructor” Occur?
When working on a basic Angular project in VS Code, you may see a message that looks like this.
Property 'make' has no initializer and is not definitely assigned in the constructor
Here is your code when having trouble with it:
makes: any[];
cars = {};
This problem happens since TypeScript 2.7 contains a strict class checking feature that requires all properties to be initialized in the constructor.
How To Fix The Error “Property ‘…’ has no initializer and is not definitely assigned in the constructor”?
Option 1: Modify strictPropertyInitialization false
- Simply access the tsconfig.json file on your computer.
- To fix the compilation problem, change “strictPropertyInitialization” to false.
- Otherwise, you’ll have to set up all of your variables.
- Now, you have fixed your error.
Option 2: Simply set up the array
Here, you should only confirm the array inside the constructor if you want it to be initialized. Consider the following instance:
makes: any[] = [];
constructor(private makeService: MakeService) {
// Initialization inside the constructor
this.makes = [];
}
Option 3: Simply include the ! as a postfix
This problem happens since TypeScript 2.7 contains a strict class checking feature that requires all properties to be initialized in the constructor. Adding the ! as a postfix to the variable name is a workable solution. Here’s how to do it: makes!: any[];
makes!: any[];
Option 4: Add specific assignment assertion to property
Excepting the solutions mentioned above, there is another solution for you to solve the error “Property ‘…’ has no initializer and is not definitely assigned in the constructor”. It is adding specific assignment assertion to property.
If you anticipate assigning the property at a later date.
The property should provide a clear assignment assertion. For example, employees.
employees!: Employee[];
After the variable name, you must add the exclamation mark (!) symbol to add the definite assignment assertion.
Remember that you are only getting around the compiler error, so make sure the property is truly assigned before using it. If not, a run-time TypeError will occur.
Conclusion
We hope you will enjoy our article about fixing the error “Property ‘…’ has no initializer and is not definitely assigned in the constructor”. We know that this error can be frustrating to deal with, so we hope that our information has helped you fix it. If you have any other questions or concerns about working with Conda, please leave a comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Leave a comment