请看这段代码:
interface A<T> {
method1(): A<T>;
}
interface B<T extends Function> extends A<T> {
method2(): B<T>;
}
var foo: A<Function>;
foo.method1();
foo.method2();
我希望foo
与该B<Function>
类型兼容,但我得到了error TS2339: Property 'method2' does not exist on type 'A<Function>'
. 我可以B
以某种方式重写接口以使其工作吗?
实际上,我正在尝试修复 lodash 的类型_.memoize
:
// This should be OK. The type of result1 should be compatible with aFunction.
var result1 = _(aFunction).memoize().value();
// And this should be an error.
var result2 = _(aNonFunctionValue).memoize().value();
更新。基本上,我的问题是:我是否可以编写这样一个通用方法,A<T>
只有当T
它是其他类型的子类型时它才可用U
?