Imagine you have the following lines of TypeScript code:
type fruit = "apple" | "banana" | "pear"
type color = "red" | "yellow" | "green"
You want to define a type that includes a numeric property for each fruit and a boolean property for each color, like this:
type FruitsAndColors = {
[key in fruit]: number;
[key in color]: boolean
}
However, you encounter an error message stating "A mapped type may not declare properties or methods", even though the code compiles successfully. What is really going on here?
To work around this issue, you can do something like this:
type FruitsAndColors = {
[key in fruit]: number;
} & {
[key in color]: boolean
}
But it would be helpful to understand the root cause of the problem.