2

code from the tutorial

In the above code, I want to declare an empty Ingredient array instead of ingredients: [ new Ingredient('Apple', '5')]

In the tutorial that I am doing, they don't show the case of the empty array. Is there a way to do it or do I always have to declare some values to let the angular know the type of the array?

0

3 Answers 3

3

This should also work

    const initialState = {
      ingredients: new Array<Ingredient>()
    };
Sign up to request clarification or add additional context in comments.

Comments

2

You need to declare the type of your state:

type State = {
  ingredients: Ingredient[];
}

and then set the type of initialState to State:

const initialState: State = {
  ingredients: [],
}

Comments

2

There are many ways to do it really, try this:

let ingredients: Ingredient[] = [];
const initialState = {
    ingredients: ingredients
}

Not sure why you need to define it inside an object, but this will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.