假设我有以下类型脚本代码:
type fruit = "apple" | "banana" | "pear"
type color = "red" | "yellow" | "green"
我想创建一个类型,每个水果都有一个数字属性,每个 colored颜色 都有一个布尔属性,比如
type FruitsAndColors = {
[key in fruit]: number;
[key in color]: boolean
}
不幸的是,这个错误会显示消息"映射类型可能不会声明属性或方法",但它编译得很好.这里到底发生了什么?
我可以用这样的方法来解决这个问题
type FruitsAndColors = {
[key in fruit]: number;
} & {
[key in color]: boolean
}
但我想知道真正的问题是什么.