![](/img/trans.png)
[英]TypeScript: Infer specific types of arguments and return values of functions in a mapped object
[英]Using mapped types to return object of return types of functions
我有一个函数,它将一个对象作为参数并返回一个新对象。 对象参数将是返回任意类型的函数的对象,而我函数的返回类型将是与对象参数具有相同键的对象,但是值将是对象参数中各个键的返回类型。
现在要抓住的是,来自object参数的函数可能会引发错误,我不想在流程的中间中断执行,而是收集所有失败的键,然后引发自己的自定义错误。
这是我到目前为止的内容:
type ObjectParamFn = <T>(...arbitrary: any[]) => T | never;
type ObjectParam = { [key: string]: ObjectParamFn };
type MyMethodReturnType<T extends ObjectParam> = {
[P in keyof T]: ReturnType<T[P]>;
}
function myMethod<T extends ObjectParam>(
param: T
): MyMethodReturnType<T> | never {
const returnValue = {} as MyMethodReturnType<T>;
const errors = [];
for (let propName in param) {
if (param.hasOwnProperty(propName)) {
try {
const value = param[propName](/* arbitrary */);
returnValue[propName] = value; // TS ERROR HERE: Type '{}' is not assignable to type 'ReturnType<T[Extract<keyof T, string>]>'.
} catch (err) {
errors.push(propName);
}
}
}
if (errors.length) {
throw new Error('foo');
}
return returnValue;
}
我标记了我收到的TS错误的位置,但将其分成自己的一行:
Type '{}' is not assignable to type 'ReturnType<T[Extract<keyof T, string>]>'.
我觉得自己已经接近解决方案,但不能完全克服最后的困难。 任何帮助将不胜感激。
我可以通过消除ObjectParamFn
的泛型来实现此目的,因为在我的代码库中,我实际上知道返回类型的类,因此我将其定义为一个并从那里开始工作。
type Primitive = string | number | boolean | null;
type ObjectParamFn = (...arbitrary: any[]) => Primitive | never;
type ObjectParam = { [key: string]: ObjectParamFn };
type MyMethodReturnType<T extends ObjectParam> = {
[P in keyof T]: ReturnType<T[P]>;
}
function myMethod<T extends ObjectParam>(
param: T
): MyMethodReturnType<T> | never {
let returnValue = {} as { [key: string]: any };
const errors = [];
for (let propName in param) {
if (param.hasOwnProperty(propName)) {
try {
returnValue[propName] = param[propName]();
} catch (err) {
errors.push(propName);
}
}
}
if (errors.length) {
throw new Error('foo');
}
return returnValue as MyReturnType<T>;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.