typescript - 如何在打字稿中使用 lodash mapValues
问题描述
我有如下代码:
interface Item {
hours: number,
minutes: number,
}
interface DataPoint {
length: number,
conversation: {
a: number,
b: number,
[key: string]: number,
}
}
interface DecoratedDataPoint {
length: Item,
conversation: {
a: Item,
b: Item,
[key: string]: Item,
}
}
我有一个像下面这样使用 lodash函数的mapValues
函数
function dataPointItem(val: number, total: number): Item {
return {
hours: Math.floor(val/60),
minutes: val%60,
percentage: Math.round((val/total)*100),
}
}
function decorateDataPoint(dataPoint: DataPoint, total: number): DecoratedDataPoint {
return {
length: dataPointItem(dataPoint.length, number)
conversation: _.mapValues(dataPoint.conversation, _.partialRight(dataPointItem, total))
}
}
我期望实现的是将计算Item
值分配给来自类型a
的b
键。conversation
DecoratedDataPoint
但是,此代码不起作用,并且出现错误:
Type 'Dictionary<Item>' is not assignable to type '{ a: Item; b: Item; }'.
Property ‘a’ is missing in type 'Dictionary<Item>'.
我检查了_.mapValues(....)
调用的结果,它返回了符合Item
外观的正确结构化对象。
关于这段代码中有什么错误的任何想法?
解决方案
推荐阅读
- python - 使用 64 位训练的 Scikits-Learn RandonForest 并在 64 位 PyCharm 中打开时出错
- swift - 如何在保持 LiDAR 调试网格的同时禁用摄像头馈送?
- c - 多个目标但相同的依赖项
- python - Python Pandas - 将 2D df 转换为 3D
- java - java中将JSON数组字符串转换为列表的通用方法
- amazon-web-services - 用于 prod 的 k8s 高可用性配置边缘案例
- r - r:在特定条件下将日期添加到日期变量
- javascript - 你如何只映射索引的一部分?反应
- python - 如何在python中构建一个函数来解决:如果X公司的标签一年一次从0变为1,那么我们返回1,否则返回0?
- python - pandas.read_csv() 不加载特殊字符