【发布时间】:2020-09-01 17:26:08
【问题描述】:
我正在使用 typescript,我需要 decare 一个类型来描述这种数据格式:
{
"apple": [{"color": "red","taste": "good"}, {"color":"green", "taste": "bad"}],
"banana":[{"color": "yellow","taste": "okay"}]
}
这就是我现在所拥有的,我声明了一个 Fruit 类型并使用 Record 将其制作为一个映射,然后我添加了字符串作为键(这将是水果名称,如苹果、香蕉)并声明了一个类型/接口 @ 987654322@ class 作为 Fruit 的值。
type Fruit = Record<string, FruitDescription>
interface FruitDescription {
color: string,
taste: string
}
通过这种设置,我无法推断出上述数据的类型。
谁能给我更好的建议来解决这类问题?谢谢你的帮助。
【问题讨论】:
-
最难的问题是那些你试图找出错误但它不存在的问题。我认为这很好,只是你在 FruitDescription 之前缺少界面
-
> 但我感觉有些不对劲 是不是意味着它可以编译了?还是只是一种感觉?
-
FruitDescription 应该是一个接口或类型
-
这是一张通用地图怎么样?
标签: javascript typescript