为传递给 Ramda 的匿名函数正确设置 TypeScript 类型

Properly setting TypeScript typings for anonymous functions passed to Ramda

我正在尝试找出 Ramda 食谱方法的正确类型 mapKeys 它不会在不抛出错误的情况下进行转换。

问题

错误 fn:

Argument of type '{}' is not assignable to parameter of type '(a: string) => string'. Type '{}' provides no match for the signature '(a: string): string'.

我从R.adjust的打字可以看出它使用了泛型,我根据错误尝试了(a: string) => string,这应该是正确的打字,以及其他几种变体,例如(a: string) => string[].

有人可以指出匿名函数的 fn 参数应该是什么来修复输入错误吗?

这真的很容易复制,只需使用 VSCode 将示例粘贴到 TypeScript 项目并通过 npm 安装 Ramda,我将 R.adjust 的输入作为参考。

例子

import * as R from 'ramda';

export const mapKeys = R.curry(
  (fn: ???, obj: { [k: string]: any } | { [k: number]: any }) =>
    R.fromPairs(
      R.map(
        R.adjust(fn, 0), // <--- Error: tried typings for `adjust`
        R.toPairs(obj)
      )
    )
);

Ramda 类型参考

adjust<T>(fn: (a: T) => T, index: number, list: T[]): T[];
adjust<T>(fn: (a: T) => T, index: number): (list: T[]) => T[];

我在 Github for types/npm-ramda and a fix 上发布了一个问题,已合并到下一个 Ramda 类型版本的 master 中。