如何将 lodash mapValues 与 typescript 一起使用

ben*_*tek 5 typescript lodash

我有如下代码:

\n\n
  interface Item {\n    hours: number,\n    minutes: number,\n  }\n\n  interface DataPoint {\n    length: number,\n    conversation: {\n      a: number,\n      b: number,\n      [key: string]: number,\n    }\n  }\n\n  interface DecoratedDataPoint {\n    length: Item,\n    conversation: {\n      a: Item,\n      b: Item,\n      [key: string]: Item,\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有一个像下面这样的函数,它使用了 lodashmapValues函数

\n\n
\n  function dataPointItem(val: number, total: number): Item {\n    return {\n      hours: Math.floor(val/60),\n      minutes: val%60,\n      percentage: Math.round((val/total)*100),\n    }\n  }\n\n  function decorateDataPoint(dataPoint: DataPoint, total: number): DecoratedDataPoint {\n    return {\n      length: dataPointItem(dataPoint.length, number)\n      conversation: _.mapValues(dataPoint.conversation, _.partialRight(dataPointItem, total))\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

我期望实现的是将计算Item值分配给类型a并从类型b中分配键。conversationDecoratedDataPoint

\n\n

但是,此代码不起作用,并且出现错误:

\n\n
  Type 'Dictionary<Item>' is not assignable to type '{ a: Item; b: Item; }'.\n    Property \xe2\x80\x98a\xe2\x80\x99 is missing in type 'Dictionary<Item>'.\n
Run Code Online (Sandbox Code Playgroud)\n\n

我检查了调用的结果_.mapValues(....),它返回符合Item外观的正确结构的对象。

\n\n

这段代码中有什么错误的想法吗?

\n