【问题标题】:How to fix Ramda drop dropLast type error in pipe?如何修复管道中的 Ramda drop dropLast 类型错误?
【发布时间】:2020-09-12 21:41:35
【问题描述】:

我在打字稿中使用 ramda 使用下面的代码,它一直显示类型不匹配的错误:

R.pipe(R.drop(2), R.dropLast(5))("hello world");

TS2769:没有重载匹配此调用。最后一个重载给出了以下错误。 '{ (xs: string): string; 类型的参数(xs: 只读未知[]): 未知[]; }' 不能分配给类型为 '(x0: readonly unknown[], x1: unknown, x2: unknown) => string' 的参数。参数“xs”和“x0”的类型不兼容。类型 'readonly unknown[]' 不可分配给类型 'string'。

应该如何解决这个问题?

【问题讨论】:

    标签: javascript typescript pipe ramda.js drop


    【解决方案1】:

    似乎类型推断需要一些额外的帮助才能让它知道您希望将类型缩小到 string

    例如

    R.pipe<string, string, string>(R.drop(2), R.dropLast(5))("hello world")
    // or
    R.pipe(R.drop(2) as (str: string) => string, R.dropLast(5))("hello world")
    

    【讨论】:

    • 很奇怪,我们为什么要这么做?为什么打字稿无法弄清楚?
    • 我怀疑这是pipe 的各种可能类型签名与drop 函数的组合,可以采用列表或字符串。例如,R.drop(2, R.dropLast(5, "hello world")) 类型检查没有错误。
    猜你喜欢
    • 2020-02-03
    • 2021-12-22
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多