我有一個具有一系列值的物件:
const my_enum = {
value1: 'x-value-1',
value2: 'x-value-2'
}
我想使用 'x-value-1' 和 'x-value-2' 作為介面或型別的鍵,例如
interface my_interface {
'x-value-1': string;
'x-value-2': string;
}
但是,我不想使用我的列舉來確定鍵,所以我可以在其余代碼中使用它們,如果我想更改我的鍵,我不需要擔心我的字串到處都是地方。有沒有辦法做類似下面的事情?
interface my_interface {
`${value1}`: string;
`${value2}`: string;
}
uj5u.com熱心網友回復:
嘗試這個
const TextType = "Text"; // or "Message" etc
const TitleType = "Title";
const PriorityType = "Priority";
type Notification {
[TextType]?: string
[TitleType]?: string
[PriorityType]?: number
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445929.html
