【问题标题】:fp-ts filterWithIndex "index signatures are incompatible"fp-ts filterWithIndex "索引签名不兼容"
【发布时间】:2022-01-10 15:23:15
【问题描述】:

通过fp-ts 学习打字稿会给我带来麻烦。

我收到以下错误:

类型 'Record' 不可分配给类型 'Timestamps'。 索引签名不兼容。 类型“数字”不可分配给类型“时间戳”。(2322)

代码如下:

import * as R from "fp-ts/dist/esm/Record";

interface Timestamp {
  date: number
  id: string
}

interface Timestamps {
  [key: string]: Timestamp
}

const MyFunction = (dates: Timestamps): Timestamps => {
    const predicate = (s: string, v: number) => s === "date" && v > 0 // 0 milliseconds
    return R.filterWithIndex(predicate)(dates)
}

我应该改变什么才能让我的代码工作?

【问题讨论】:

    标签: typescript functional-programming fp-ts


    【解决方案1】:

    您的谓词需要对Timestamps 进行操作,而不是numbers

    const predicate = (s: string, v: Timestamp) =>
      s === "date" && v.date > 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2021-08-01
      • 2022-09-25
      • 1970-01-01
      相关资源
      最近更新 更多