javascript - 打字稿声明地图类型
问题描述
我正在使用打字稿,我需要定义一种类型来描述这种数据格式:
{
"apple": [{"color": "red","taste": "good"}, {"color":"green", "taste": "bad"}],
"banana":[{"color": "yellow","taste": "okay"}]
}
这就是我现在所拥有的,我声明了一个 Fruit 类型并使用 Record 将其制作为一个映射,然后我添加了字符串作为键(这将是水果名称,如苹果、香蕉)并将一个类型/接口FruitDescription
类声明为 Fruit 的值.
type Fruit = Record<string, FruitDescription>
interface FruitDescription {
color: string,
taste: string
}
通过这种设置,我无法推断出上述数据的类型。
有人可以给我更好的建议来解决这类问题吗?谢谢你的帮助。
解决方案
看起来您的类型注释中缺少一系列水果描述。
type Fruit = Record<string, FruitDescription[]>
推荐阅读
- window - 如何在 dolphindb 中将 1 分钟的数据聚合为 30 分钟或 1 小时的平均数据
- flutter - docker image 中的“flutter channel stable”失败:将页面发布到 gitlab 时出现 cirrusci
- https - JWKS 端点是否需要 https / TLS?
- git - 将单个代码库部署到多个客户端
- python - Odoo 14 条码模块使用 get_all_products_by_barcode 方法性能不佳
- eclipse - Cyclone DDS Package 配置文件丢失
- android - 在片段之间重用 ViewModel
- ios - 如何在 Swift 的模态表前呈现 UIAlertController
- django - 新表中的 Django ForeignKey 默认值
- r - x 下标在 mutate 中越界