【发布时间】:2020-08-01 19:58:16
【问题描述】:
使用 Typescript 我想将 Enum 转换为
type keys = "key1"| "key2"
// or
type values = "value1" | "value2"
我可以在课堂上做到这一点
class C { a: number; b: string;}
type Keys = keyof C; // "x" | "y"
type Values = C[keyof C]; // string | number
我想做的事
enum Roles {Admin = "admin", Client = "client"};
? --> type Keys = ...; // "Admin" | "Client"
? --> type Values = ...; // "admin" | "client";
这可能有点帮助
enum Roles {Admin = "admin", Client = "client"};
type values = {[K in Roles]: K} // { admin: Roles.Admin; client: Roles.Client; }
【问题讨论】:
标签: typescript types enums type-conversion