通用类型'ReturnType'需要1个类型参数。ts(2314)

问题描述 投票:0回答:1

我最近从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个类型的参数?似乎没有替代品$Call

javascript reactjs typescript flowtype
1个回答
0
投票
ReturnType<T>仅接受单个类型参数,即函数类型。在您的情况下,功能类型为ReturnType<T>。整个TypeScript表达式将为typeof toObject,并将解析为ReturnType<typeof toObject>,因为您将返回any的结果,该结果在您的代码中键入为reduce
© www.soinside.com 2019 - 2024. All rights reserved.