I have a constant object like this
const rack = {
topShelf: {
colors: ["red", "green"],
},
middleShelf: {
colors: ["yellow", "blue"],
},
bottomShelf: {
colors: ["orange", "purple"],
},
}
And I want to create a union type like:
type Color = "red" | "green" | "yellow" | "blue" | "orange" | "purple"
The rack object is just an example. In a real application, the object has around 30 fields.
I know how to do it manually
type Color =
| typeof rack["topShelf"]["colors"][number]
| typeof rack["middleShelf"]["colors"][number]
| typeof rack["bottomShelf"]["colors"][number]
But it is error-prone and I'm sure there is a way to get this union type inferred by Typescript.