我最近从 Flow 转移到 Typescript,在转换一些代码库时,我遇到了几个错误,大多数错误已记录或被实用程序类型包替换,但是我找不到帮助我解决问题的文档或答案下面的代码。
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
在您的代码中。
关于javascript - 泛型类型 'ReturnType' 需要 1 个类型参数.ts(2314),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149511/