[英]how to define attributes for all properties of an object simultaneously (or set default)
[英]Define a pattern for all properties of an object
我在我的應用程序中定義了一個 Product 接口,如下所示:
export interface Product {
// other properties here
datesForComparison?: {};
}
基本上我希望datesForComparison
的結構是這樣的
{
"2020-01-01": {
price: 10,
unitPrice: 5,
percent: 5
},
"2020-01-02": {
price: 4,
unitPrice: 2,
percent: -1
},
...
}
是否可以在 Typescript 中定義這種重復的屬性模式? 謝謝!
我認為您正在尋找索引簽名:
export interface Product {
datesForComparison?: {
[date: string]: {
price: number;
unitPrice: number;
percent: number;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.