Home > Net >  Typescript Angular - Recieving Type is missing following properties from type
Typescript Angular - Recieving Type is missing following properties from type

Time:06-07

So I'm trying to setup a model and keep getting the mentioned area. Screenshots attached show my syntax - I assumed it was an error from not passing the 'name' , 'amount' but in my array I give two new values with both of those.

EDIT - Can' post images so code below.

  ingredients: Ingredient = [
new Ingredient ('Apples', 5),
new Ingredient ('Tomatos', 10)];
export class Ingredient{

constructor(public name: string, public amount: number){ }}

CodePudding user response:

The way you're declaring it "ingredients" variable should be an ingredient and not an array/list of ingredients.

So:

const ingredient: Ingredient = new Ingredient ('Apples', 5)

or

const ingredients: Ingredient[] = [
  new Ingredient ('Apples', 5),
  new Ingredient ('Tomatos', 10)
];

To specify an array type you can also use:

const ingredients: Array<Ingredient> = [...]
  • Related