【发布时间】:2018-05-09 11:39:13
【问题描述】:
我认为这是正确实现泛型的问题,但我不确定。
我创建了一个 Github gist 来代表这里的问题: https://gist.github.com/ORESoftware/66b72b4b85262d957cb03ad097e4743e
假设我有这个超类:
class A {
foo(): A {
return this;
}
}
还有几个子类,例如一个看起来像这样:
class B extends A {
bar(): B {
return this;
}
}
如果我这样做了
new B().foo().bar()
这将在运行时工作,但它不能使用 TypeScript 编译。这是因为foo() 声明返回类型为A,而不是类型B。
如何返回 this 的类型,而不是声明 foo() 始终返回类型 A?
我试过这个:
但我收到此错误:
【问题讨论】:
标签: typescript generics typescript2.0 typescript-generics