在 TypeScript 中自动覆盖 child class 中几个函数的 return 类型
Automatically override return type of several functions in a child class in TypeScript
我有一个 class 有几个函数 return 一个新的实例,我想扩展 class 并让这些函数 return 成为一个实例child class 代替。例如
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"
这就是我想要完成的,但理想情况下我不需要在 class B
中重新实现 next()
和 double()
并手动转换 return 类型。我尝试了一种使用泛型的方法,它让我只能覆盖 class 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 之间的关系?
您可以使用多态 this。
class A {
constructor(public val) {
}
getAnother(val): this {
return new A(val);
}
next(): this {
return this.getAnother(this.val + 1);
}
double(): this {
return this.getAnother(this.val * 2);
}
}
class B extends A {
getAnother(val): this {
return new B(val);
}
getValAsStr(): string {
return String(this.val);
}
}
(未经测试)
我有一个 class 有几个函数 return 一个新的实例,我想扩展 class 并让这些函数 return 成为一个实例child class 代替。例如
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"
这就是我想要完成的,但理想情况下我不需要在 class B
中重新实现 next()
和 double()
并手动转换 return 类型。我尝试了一种使用泛型的方法,它让我只能覆盖 class 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 之间的关系?
您可以使用多态 this。
class A {
constructor(public val) {
}
getAnother(val): this {
return new A(val);
}
next(): this {
return this.getAnother(this.val + 1);
}
double(): this {
return this.getAnother(this.val * 2);
}
}
class B extends A {
getAnother(val): this {
return new B(val);
}
getValAsStr(): string {
return String(this.val);
}
}
(未经测试)