I have an object in which both the properies and values correspond to their respective types:
type Car = "toyota" | "mazda" | "suzuki" | "porsche";
type Color = "white" | "black" | "yellow" | "red";
const cars = {
toyota: "red",
porsche: "white",
};
I know I can assign a type to the property like so:
const cars: {
[key in Car]: string;
} = {
toyota: "red",
porsche: "white",
};
This works but I would like the value to only be of type Color. The following does not work:
const cars: {
[key in Car]: [key in Color];
} = {
toyota: "red",
porsche: "white",
};
Why doesn't the above code work, and how may I define a type for both property name and value?