[英]How to extract the type of enumerated value use typescript
enum code {
a = 10,
b = 20,
c = "abc"
}
Now I have an enum value and I now want to get a union type现在我有一个枚举值,我现在想要一个联合类型
type IWantGet = 10 | 20 | 'abc'
Excuse me, do you have any good solutions?请问,有什么好的解决办法吗?
This will become possible in TypeScript 4.8 when the conversion of string literals to numbers is added.在 TypeScript 4.8 中,当添加了将字符串文字转换为数字时,这将成为可能。
type IWantGet = `${code}` extends infer U
? U extends `${infer S extends number}`
? S
: U
: never
// type IWantGet = "abc" | 10 | 20
For now you can only have a union of string literal types.现在你只能有一个字符串文字类型的联合。
type IWantGet = `${code}`
// type IWantGet = "10" | "20" | "abc"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.