close

[Solved] Property ‘…’ has no initializer and is not definitely assigned in the constructor

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 ?

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 ?

  1. 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[];

  2. 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

  1. Just Open your tsconfig.json file.
  2. Now set “strictPropertyInitialization”: false
  3. to get rid of the compilation error.
  4. Otherwise you need to initialize all your variables.
  5. 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

Please Rate Our Solution to Motivate Us For Give Our Best

Leave a Comment