【发布时间】:2021-05-29 00:06:13
【问题描述】:
我有一个类,它有几个返回自身新实例的函数,我想扩展该类并让这些函数返回子类的实例。例如
class A {
constructor(public val) {
}
getAnother(val): A {
return new A(val);
}
next(): A {
return this.getAnother(this.val + 1);
}
double(): A {
return this.getAnother(this.val * 2);
}
}
class B extends A {
getAnother(val): B {
return new B(val);
}
next(): B {
return <B>super.next();
};
double(): B {
return <B>super.double();
};
getValAsStr() {
return String(this.val);
}
}
let b = new B(1);
console.log(b.next().getValAsStr()); // logs "2"
这完成了我想要完成的工作,但理想情况下,我不需要在 B 类中重新实现 next() 和 double() 并手动转换返回类型。我尝试了一种使用泛型的方法,它只允许我在 B 类中覆盖 getAnother(),但它破坏了 A 和 B 之间的多态关系
abstract class I<TResult> {
constructor(public val) {
}
abstract getAnother(val): TResult;
next(): TResult {
return this.getAnother(this.val + 1);
}
double(): TResult {
return this.getAnother(this.val * 2);
}
}
class A extends I<A> {
getAnother(val): A {
return new A(val);
}
}
class B extends I<B> {
getAnother(val): B {
return new B(val);
}
getValAsStr() {
return String(this.val);
}
}
有没有一种方法比我的第一种方法更干净,并且在 TypeScript 中保持 A 和 B 之间的关系?
【问题讨论】:
标签: typescript inheritance covariance typescript-generics