【发布时间】:2020-09-04 18:38:01
【问题描述】:
我有枚举:
enum DemoEnum {
a = 'EnumValueA',
b = 'EnumValueB'
}
我想从我的枚举值创建type Type = 'EnumValueA' | 'EnumValueB'。
我该怎么做?
我目前的状态是“钥匙”类型:
type Type = keyof typeof DemoEnum // 'a' | 'b'
例如,我想在我的 react 道具中使用它。
type Props {
value: 'EnumValueA' | 'EnumValueB',
}
万一使用<MyComponent value='EnumValueA'>
type Props {
value: DemoEnum,
}
我收到一个错误Type .. is not assignable to DemoEnum
【问题讨论】:
-
你为什么要这个?你能在需要这种类型的地方展示一些代码吗?根据用例,您可以只使用
DemoEnum作为类型,因为它是您要查找的联合的子类型。 -
在反应道具中(更新)。
标签: typescript enums