![](/img/trans.png)
[英]How to declare an object of arrays of custom type in typescript
[英]How to declare a specific type of empty array inside a object using typescript?
您需要聲明您的 state 的類型:
type State = {
ingredients: Ingredient[];
}
然后將initialState
的類型設置為State
:
const initialState: State = {
ingredients: [],
}
有很多方法可以做到這一點,試試這個:
let ingredients: Ingredient[] = [];
const initialState = {
ingredients: ingredients
}
不確定為什么需要在 object 中定義它,但這會起作用。
這也應該工作
const initialState = {
ingredients: new Array<Ingredient>()
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.