typescript - typescript 中的数组减少和可选值

标签 typescript

假设我有以下可折叠界面:

export interface Foldable<F> {
  reduce: <A>(fn: (b: A, a: A) => A, initial: A, foldable: F) => A;
}

然后我想为数组实现它:

export const getArrayFold = <A>(): Foldable<Array<A>> => {
  return {
    reduce: (fn, initial, array) => {
      return array.reduce(fn, initial);
    }
  };
};

但是编译器提示:

Argument of type '(b: A, a: A) => A' is not assignable to parameter of type '(previousValue: A, currentValue: A, currentIndex: number, array: A[]) => A'. Types of parameters 'a' and 'currentValue' are incompatible. Type 'A' is not assignable to type 'A'. Two different types with this name exist, but they are unrelated.

我不明白为什么这里有两种不同类型的A

最佳答案

有两个错误:

  • 您需要提供数组类型。您无法从单个通用 Array<T> 获得它, 你需要同时介绍 TArray<T> .
  • reduce 使用的函数类型是不够的。正确一个:(previousValue: A, currentValue: F) => A

解释:

如果您提供具有类型(例如 string)的 initial 值来减少函数,previousValue 参数始终与 inital 相同。

见官方TypeScript reduce declaration :

interface Array<T> {
    reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: ReadonlyArray<T>) => U, initialValue: U): U;
}

完整代码(重构)

interface Foldable<F, T> {
    reduce: <A>(
        fn: (previousValue: A, currentValue: T) => A,
        initial: A,
        foldable: F
    ) => A;
}

const getArrayFold = <T>(): Foldable<T[], T> => ({
    reduce(fn, initial, array) {
        return array.reduce(fn, initial);
    }
});

// Real implementation usage
const array: number[] = [1, 2, 3]
const initial: string = "";
const fn: (previousValue: string, currentValue: number) => string = (a, b) => a + b;

const newValue: string = getArrayFold().reduce(fn, initial, array);

参见 TypeScript playground 上的代码

关于typescript - typescript 中的数组减少和可选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031247/

相关文章:

javascript - 错误 : Member 'feedbackFormDirective' implicitly has an 'any' type in angular

checkbox - typescript 如何判断该元素是复选框,所以 element.checked 不是红色下划线

angular - 如何在 typescript 中使用 map 运算符上的类型?

javascript - 在执行操作之前等待 2 个异步 API 调用的结果

node.js - 如何将 TypeScript 1.6 与 Visual Studio Code 结合使用以获得生成器支持?

javascript - Jest 在导入语句中失败并显示 "Unexpected token *"

typescript - Cypress cucumber 预处理器自定义参数类型与 typescript

Javascript 将变量添加到正则表达式

typescript - Cesium 与 Webpack 和 Typescript : ReferenceError: Cesium is not defined

angular - ESRI 加载程序搜索小部件聚焦 angular 7 的问题