我有如下代码:
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
的{{1}}和b
密钥分配给计算的conversation
值。
但是此代码不起作用,并且出现错误:
DecoratedDataPoint
我已经检查了 Type 'Dictionary<Item>' is not assignable to type '{ a: Item; b: Item; }'.
Property ‘a’ is missing in type 'Dictionary<Item>'.
调用的结果,它返回的结构正确的对象遵循_.mapValues(....)
的外观。
任何有关此代码中的错误的想法吗?