[英]Dynamic object keys in TypeScript
在 TypeScript 中設置動態對象的類型時遇到問題,因為我創建的對象具有動態鍵,而三個不是。
這是我定義對象類型的方式:
interface GraphReturns {
[key: string]: {
‘%’: number,
value: number,
‘%Acumulated’: number
},
total: number,
‘total_%’: number,
date: string
}
我得到的錯誤:
我在這里只看到兩個解決方案。
這個似乎更“正確”,但我不確定你是否可以使用它
interface CallItSomehow {
'%': number;
value: number;
'%Acumulated': number;
}
interface GraphReturns {
total: number,
'total_%': number,
date: string
other: Record<string, CallItSomehow>, // In this case you had to write information into another field
}
或者你可以做
interface GraphReturns {
total: number,
'total_%': number,
date: string
[key: string]: {
'%': number,
value: number,
'%Acumulated': number
} | string | number,
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.