Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I declare a variable without initiate it in ANGULAR?

I’m new to Angular and I try to declare a new object type or a simple string variable but I get an error

this_is_variable:string;
recipe : Recipe;

the errors:

Property ‘recipe’ has no initializer and is not definitely assigned in the constructor.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Property ‘this_is_variable’ has no initializer and is not definitely assigned in the constructor.

In the above example Recipe is a class which I have import it

export class Recipe{
public name:string;
public description : string;
public imagePath: string;
constructor(name:string="none", desc:string="none" , imagePath:string="none"){
    this.name= name;
    this.description=desc;
    this.imagePath= imagePath;


}

}

>Solution :

The reported error is from typescript compiler. Set the property strictPropertyInitialization to false in tsconfig.json file of Angular app solution to avoid this compiler error.

{
  "compilerOptions": {
    // ... rest
    "strictPropertyInitialization": false
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading