[英]How to type a string as an Object property-key in a typescript Object interface?
[英]Interface or type with object as key in typescript
我有一个带有一系列值的 object:
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;
}
但是,我不想使用我的枚举来确定键,所以我可以在代码的 rest 上使用它们,如果我想更改我的键,我不需要担心我的字符串地方。 有没有办法做类似下面的事情?
interface my_interface {
`${value1}`: string;
`${value2}`: string;
}
尝试这个
const TextType = "Text"; // or "Message" etc
const TitleType = "Title";
const PriorityType = "Priority";
type Notification {
[TextType]?: string
[TitleType]?: string
[PriorityType]?: number
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.