簡體   English   中英

為對象的所有屬性定義一個模式

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM