我有如下代码:
\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 }\nRun Code Online (Sandbox Code Playgroud)\n\n我有一个像下面这样的函数,它使用了 lodashmapValues函数
\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 }\nRun Code Online (Sandbox Code Playgroud)\n\n我期望实现的是将计算Item值分配给类型a并从类型b中分配键。conversationDecoratedDataPoint
但是,此代码不起作用,并且出现错误:
\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>'.\nRun Code Online (Sandbox Code Playgroud)\n\n我检查了调用的结果_.mapValues(....),它返回符合Item外观的正确结构的对象。
这段代码中有什么错误的想法吗?
\n| 归档时间: |
|
| 查看次数: |
1679 次 |
| 最近记录: |