问题是
在TypeScrip中,我们可以使用extends
来断言一个泛型参数作为另一种类型的‘子类型’.例如.
// class Base {}
// class Child extends Base {}
// edited:
class Base { a = 1 }
class Child extends Base { b = 2 } // Child is a sub class of Base
class Test {
// assert T is a subtype of Base
hello<T extends Base>() { ... }
}
new Test().hello<Child>();
但是如何将泛型参数断言为另一种类型的"超类型"呢?例如.
class Test2 {
// HERE, how to ensure T is a super type of Child
hello<T ??? Child>() { ... }
}
new Test2().hello<Base>();
用例
// class Actions<T = unknown> {
// edited:
class Actions<T = never> {
execs: (() => T)[]
append(exec: () => T) {
this.execs.push(exec)
return this;
}
// HERE, how to ensure NewT is a super type of T?
setT<NewT ??? T>() {
return this as Actions<NewT>;
}
}
// to make the following work
new Actions()
.setT<Child>()
.append(() => new Child())
// before this line, all exec should return Child
// after this line, all exec should return Base
.setT<Base>()
.append(() => new Base())
.execs.map(e => e()) // => Base[]
如果我们仍然像这样使用extends
:
setT<NewT extends T>() {
return this as Actions<NewT>;
}
收到错误:
Conversion of type 'this' to type 'Actions<NewT>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Type 'Actions<T>' is not comparable to type 'Actions<NewT>'.
Type 'T' is not comparable to type 'NewT'.
'NewT' could be instantiated with an arbitrary type which could be unrelated to 'T'.ts(2352)
是的,我可以使用unknown
来转换返回类型,但是函数setT
的签名不能表达我们想要的类型约束.