I have the following example that I'd like to make it work. See comments in code:
const cars = {
"audi": {
manufacturer: "audi",
colors: ["red", "blue"]
},
"mercedes": {
manufacturer: "mercedes",
colors: ["blue", "black"]
}
} as const
type CarType = typeof cars[keyof typeof cars]
type Mapper<C extends CarType> = {
manufacturer: C["manufacturer"]
color: C["colors"][number]
}
type Car = Mapper<CarType>
const car: Car = {
manufacturer: "audi",
color: "black" // <- this is wrong, only "red" and "blue" are valid
}
As it can be seen from the example, I would like to have a color field that has only a single option and is derived from the list of valid colors.
The issue is that my Mapper type is wrong, because it does not pick out individual CarTypes, but I don't know how to write it.