How to build a type from enum values in TypeScript? How to build a type from enum values in TypeScript? typescript typescript

How to build a type from enum values in TypeScript?


Yes, you can use enum values as keys. And you can use a mapped type like the standard library's Record<K, V> to prevent repetition:

enum FooKeys {  FOO = 'foo',  BAR = 'bar',}// probably all you need, but it's a type aliastype FooType = Record<FooKeys, string>;// if you need an interface instead you can do thisinterface FooInterface extends FooType {};

And you can verify that it works:

declare const foo: FooInterface;foo.foo; // okayfoo[FooKeys.FOO]; // okayfoo.bar; // okayfoo[FooKeys.BAR]; // okayfoo.baz; // error

Does that work for you? Good luck!


@hackape 's solution is great, but I found minimal duplication extending his solution as below:

type ReverseMap<T extends Record<keyof T, any>> = {  [V in T[keyof T]]: {    [K in keyof T]: T[K] extends V ? K : never;  }[keyof T];}const Map = {  'FOO': "foo" as "foo",  'BAR': "bar" as "bar",}const reverseMap: ReverseMap<typeof Map> = Object.entries(Map).reduce((rMap, [k, v]) => {  rMap[v] = k;  return rMap;}, {} as any);export type Values = keyof typeof reverseMap; // 'foo' | 'bar';

ReverseMap implementation is well explained here


Does this answer your question?

enum FOO_BAR {    F = "foo",    B = "bar",}type FooType = Record<FOO_BAR, string>;const obj: FooType = {    // you can use enum values, better for refactoring    [FOO_BAR.F]: "action foo",    [FOO_BAR.B]: "action bar",    // or use enum values    // foo: "action foo",    // bar: "action bar",};obj[FOO_BAR.F]; // -> "action foo"obj["foo"];     // -> "action foo"// If you want partial keystype FooTypePartial = Partial<FooType>;const objPartial: FooTypePartial = {    [FOO_BAR.F]: "action foo",};objPartial["foo"]; // -> "action foo", may be undefined