泛型 'ReturnType' 需要 1 个类型参数.ts(2314)

Generic type 'ReturnType' requires 1 type argument(s).ts(2314)

我最近从 Flow 转移到 Typescript,在转换一些代码库时我遇到了几个错误,大部分都被 Utility-Types 包记录或替换,但是我找不到对我有帮助的文档或答案使用以下代码。

const toObject = (keys: { reduce: (arg0: (object: any, key: any) => any, arg1: {}) => void }) =>
  keys.reduce((object: any, key: string | number) => {
    const o = object;
    o[key] = undefined;

    return object;
  }, {});



export type Pick<
  Origin extends Record<string, any>,
  Keys extends ReadonlyArray<keyof Origin>
> = $ObjMapi<ReturnType<typeof toObject, Keys>, <Key>(k: Key) => $ElementType<Origin, Key>>;

export type TypeOrVoid = <T>(arg0: T) => T | void;

export type Diffable<O extends {}> = $ObjMap<O, TypeOrVoid>;

更具体地说,Generic type 'ReturnType' requires 1 type argument(s).ts(2314) ReturnType<typeof toObject, Keys>

上的错误

如何在保留相同功能的同时减少到 1 个类型参数?似乎没有 Flow $Call.

的替代品

ReturnType<T> 只接受一个类型参数,即函数类型。在您的例子中,函数类型是 typeof toObject。整个 TypeScript 表达式将是 ReturnType<typeof toObject> 并将解析为 any,因为您要返回 reduce 的结果,它在您的代码中键入为 any