如何在打字稿中使用lodash mapValues

时间:2019-09-09 20:27:23

标签: typescript lodash

我有如下代码:

  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(....)的外观。

任何有关此代码中的错误的想法吗?

0 个答案:

没有答案