2
这里是我的设置:如何在TypeScript中将eta-reduced泛型函数作为参数传递?
export interface Promised <d, e> {
then<o, e>(
haveData: (data: d) => o,
haveError?: (error: e) => e
): Promised<o, e>;
}
export interface Problem {
message: string;
}
export interface Nothing {
}
export function ignore<a>(value: a): a { return value; }
export var nothing: Nothing = {};
export function fail<a>(problem: Problem): a;
export function fail<a>(message: string): a;
export function fail(something: any): any {
if (typeof something === 'string') {
throw new Error(something);
} else {
throw new Error(something.message);
}
};
我需要的是ignoreOrFail
功能下面写:
export function ignoreOrFail<d>(promise: Promised<d, Problem>): Nothing {
promise.then(ignore, fail); // <--- PROBLEM WITH 'fail'
return nothing;
}
但是它打破了,因为fail
函数的类型参数无法推断出可以:
Supplied parameters do not match any signature of call target:
Call signatures of types 'typeof fail' and '(error: Problem) => Problem' are incompatible:
Type '{}' is missing property 'message' from type 'Problem'.
Type '{}' is missing property 'message' from type 'Problem'.
这可以如果我定义其中类型参数是硬编码的另一种方法是固定的:
export function failAlong(problem: Problem): Problem {
return fail<Problem>(problem);
};
export function ignoreOrFail<d>(promise: Promised<d, Problem>): Nothing {
promise.then(echo, failAlong); // <---- WORKS THIS WAY
return nothing;
}
但这是一种愚蠢的事情,不是吗?我认为会起作用的是给编译器一个这样的提示:
export function ignoreOrFail<d>(promise: Promised<d, Problem>): Nothing {
promise.then(echo, fail<Problem>);
return nothing;
}
但是这似乎超出了语法。所以这个问题:有没有一种方法可以将eta-reduced通用函数指定为参数? PS: 对于非泛型函数它不是一个问题(看ignore
)。