gpt4 book ai didi

javascript - 为对象的所有属性定义模式

转载 作者:行者123 更新时间:2023-12-02 21:49:35 26 4
gpt4 key购买 nike

我在我的应用程序中定义了一个产品接口(interface),如下所示:

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 中定义这种重复的属性模式?谢谢!

最佳答案

我认为您正在寻找 Index Signature :

export interface Product {
datesForComparison?: {
[date: string]: {
price: number;
unitPrice: number;
percent: number;
}
}
}

关于javascript - 为对象的所有属性定义模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60160114/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com