[英]How to get enum values typescript type
Not sure how to get enum values typescript type:不确定如何获取枚举值 typescript 类型:
// enum is generated and i can't change it
enum UserRole {
Alfa = 'alfa',
Beta = 'beta',
Helper = 'helper'
}
// i need to get type like this:
type Role = 'alfa' | 'beta' | 'helper'
// to use it like this:
const role: Role = 'alfa'
// what i got so far
type A1 = keyof typeof UserRole // "Alfa" | "Beta" | "Helper"
type A2 = Record<UserRole, string>
/*{
alfa: string;
beta: string;
helper: string;
}*/
// intresting that if i use A2 type defined by hand it will work
type X = {
alfa: string;
beta: string;
helper: string;
}
const x: keyof X = 'alfa'
// but if i try to get this type from A2 directly it will jump back to UserRole and wont work
const y: keyof A2 = 'alfa'
Not sure how to get enum values typescript type:不确定如何获取枚举值 typescript 类型:
// enum is generated and i can't change it
enum UserRole {
Alfa = 'alfa',
Beta = 'beta',
Helper = 'helper'
}
// i need to get type like this:
type Role = 'alfa' | 'beta' | 'helper'
// to use it like this:
const role: Role = 'alfa'
// what i got so far
type A1 = keyof typeof UserRole // "Alfa" | "Beta" | "Helper"
type A2 = Record<UserRole, string>
/*{
alfa: string;
beta: string;
helper: string;
}*/
// intresting that if i use A2 type defined by hand it will work
type X = {
alfa: string;
beta: string;
helper: string;
}
const x: keyof X = 'alfa'
// but if i try to get this type from A2 directly it will jump back to UserRole and wont work
const y: keyof A2 = 'alfa'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.