- Stackblitz with the code
export const Colors = {
Blue: 'blue',
Green: 'green',
} as const
export type Colors = typeof Colors[keyof typeof Colors]
// typeof Colors gives you the object literal above. ie const Colors
// keyof then gives you Blue & Green
// typeof Colors[Blue] = 'blue'
// typeof Colors[Green] = 'green'
function showColor(color: Colors) {
console.log(color)
return color
}
showColor('blue')
// evaluates to blue
showColor(Colors.Green)
// evaluates to green