Created
January 11, 2026 02:12
-
-
Save jonathanconway/a192942e3a4cb640a5cd4d2d321df917 to your computer and use it in GitHub Desktop.
Type utility to transform an object const map into a literal union
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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