Hello Guys, How are you all? Hope You all Are Fine. Today I am running my simple angular project But I am facing some warnings in my Vs code like Property ‘…’ has no initializer and is not definitely assigned in the constructor in Angular. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
- How This Error Property '…' has no initializer and is not definitely assigned in the constructor Occurs ?
- How To Solve Property '…' has no initializer and is not definitely assigned in the constructor Error ?
- Solution 1: Change strictPropertyInitialization false
- Solution 2: Just initialize the array
- Solution 3: Just add the
!
as a postfix - Summery
How This Error Property ‘…’ has no initializer and is not definitely assigned in the constructor Occurs ?
I have simple Angular Project Bu In VS code I have a message something Like This.
Property 'make' has no initializer and is not definitely assigned in the constructor
This is my code here I am facing this issue.
makes: any[];
cars = {};
How To Solve Property ‘…’ has no initializer and is not definitely assigned in the constructor Error ?
- How To Solve Property '…' has no initializer and is not definitely assigned in the constructor Error?
To Solve Property '…' has no initializer and is not definitely assigned in the constructor Error This error occurs because TypeScript 2.7 includes a strict class checking where all the properties should be initialized in the constructor. A workaround is to add the
!
as a postfix to the variable name. Here is how. makes!: any[]; - Property '…' has no initializer and is not definitely assigned in the constructor
To Solve Property '…' has no initializer and is not definitely assigned in the constructor Error This error occurs because TypeScript 2.7 includes a strict class checking where all the properties should be initialized in the constructor. A workaround is to add the
!
as a postfix to the variable name. Here is how. makes!: any[];
Solution 1: Change strictPropertyInitialization false
- Just Open your tsconfig.json file.
- Now set “strictPropertyInitialization”: false
- to get rid of the compilation error.
- Otherwise you need to initialize all your variables.
- Now your error must be solved.
Solution 2: Just initialize the array
Here You should Just initialize the array when you declare it inside the constructor. Here is Example.
makes: any[] = [];
constructor(private makeService: MakeService) {
// Initialization inside the constructor
this.makes = [];
}
Solution 3: Just add the !
as a postfix
This error occurs because TypeScript 2.7 includes a strict class checking where all the properties should be initialized in the constructor. A workaround is to add the !
as a postfix to the variable name. Here is how.
makes!: any[];
Summery
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also Read
thaaank you man
It’s my Pleasure to Help You Fares. Thank You For Your Valuable words.