[Typescript] Get All of an Object’s Values

Let's say we have an object as const:

const frontendToBackendEnumMap = {
  singleModule: "SINGLE_MODULE",
  multiModule: "MULTI_MODULE",
  sharedModule: "SHARED_MODULE",
} as const;

 

We want to get all values of the object as 

"SINGLE_MODULE" | "MULTI_MODULE" | "SHARED_MODULE"
 
const frontendToBackendEnumMap = {
  singleModule: "SINGLE_MODULE",
  multiModule: "MULTI_MODULE",
  sharedModule: "SHARED_MODULE",
} as const;

type BackendModuleEnum =
  typeof frontendToBackendEnumMap[keyof typeof frontendToBackendEnumMap];

type tests = [
  Expect<
    Equal<BackendModuleEnum, "SINGLE_MODULE" | "MULTI_MODULE" | "SHARED_MODULE">
  >
];

 

posted @ 2022-12-11 20:37  Zhentiw  阅读(17)  评论(0编辑  收藏  举报