[英]Use const enum keys (not values) as interface properties
In following code variable x
has properties a
and z
, but I want it to have a
and b
.在下面的代码变量
x
具有属性a
和z
,但我希望它具有a
和b
。 How can I express that in typescript?我如何用打字稿表达它?
const enum CE {
a = "a",
b = "z",
}
declare var x: Record<CE, any> // has 'a' and 'z'
Enum is variable itself, so you can query its type with typeof CE
, then get its keys with keyof
: Enum 本身就是变量,因此您可以使用
typeof CE
查询其类型,然后使用keyof
获取其键:
declare var x: Record<keyof typeof CE, any> // Record<"a" | "b", any>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.