Skip to content

Instantly share code, notes, and snippets.

@jonathanconway
Created January 11, 2026 02:12
Show Gist options
  • Select an option

  • Save jonathanconway/a192942e3a4cb640a5cd4d2d321df917 to your computer and use it in GitHub Desktop.

Select an option

Save jonathanconway/a192942e3a4cb640a5cd4d2d321df917 to your computer and use it in GitHub Desktop.
Type utility to transform an object const map into a literal union
/**
* Transform an object const map into a literal union.
*
* @example
* const Colors = {
* Red = "red",
* Green = "green",
* Blue = "blue",
* } as const;
*
* // "red" | "green" | "blue";
* type Color = TypeOfConst<typeof Colors>;
*
* @see https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types
*/
export type TypeOfConst<Const> = Const[keyof Const];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment