一个用于返回一个继承自抽象类的派生类的类的函数

huangapple go评论76阅读模式
英文:

A function to return a class which extends a derived class of an abstract class

问题

I would like to make a function to return a class which extends a derived class of an abstract class in Typescript. I think Abstract Construct Signature should be the one I should use. However, Typescript prompts "Non-abstract class 'SayHelloDerived' does not implement all abstract members of 'Base'." Here is a simplified code.

// @errors: 18052
abstract class Base {
  abstract getName(): string;
  printName() {
    console.log("a")
  }
}
class Derived extends Base {
  getName() {
    return "";
  }
}
function makeSayHelloDerived(DerivedBase: new () => Base) {
  return class SayHelloDerived extends DerivedBase {
    sayHello() {
      console.log("hello!");
    }
  }
}

const Derived2 = makeSayHelloDerived(Derived);
const derived2 = new Derived2;
derived2.sayHello();

const Derived3 = makeSayHelloDerived(Base);
const derived3 = new Derived3;
derived3.sayHello();

Typescript Playground link

The second error is expected, but the first one is not.

I expect Typescript can recognize parameter "DerivedBase" in function "makeSayHelloDerived" as a derived class of abstract "Base" class. It should not prompt any error, but it prompts now. I have googled around and searched stackOverflow here about "Abstract Construct Signature," but no luck. Did I have anything wrong?

英文:

I would like to make a funciton to return a class which extends a derived class of an abstract class in Typescript. I think Abstract Construct Signature should be the one I should use. However, Typescript prompts "Non-abstract class 'SayHelloDerived' does not implement all abstract members of 'Base'." Here is a simplified code.

// @errors: 18052
abstract class Base {
  abstract getName(): string;
  printName() {
    console.log("a")
  }
}
class Derived extends Base {
  getName() {
    return "";
  }
}
function makeSayHelloDerived(DerivedBase: new () => Base) {
  return class SayHelloDerived extends DerivedBase {
    sayHello() {
      console.log("hello!");
    }
  }
}

const Derived2 = makeSayHelloDerived(Derived);
const derived2 = new Derived2;
derived2.sayHello();

const Derived3 = makeSayHelloDerived(Base);
const derived3 = new Derived3;
derived3.sayHello();

Typescript Playground link

The second error is expected, but the first one is not.

I expect Typescript can recognize parameter "DerivedBase" in function "makeSayHelloDerived" as a derived class of abstract "Base" class. It should not prompt any error, but it prompts now. I have googled around and search stackOverflow here about "Abstract Construct Signature," but no luck. Did I have anything wrong?

答案1

得分: 0

The easy solution would be to change the type of the parameter to new () => Derived, but I suppose that's not what you want - you want to accept any non-abstract class that inherits from Base.

我想,一个简单的解决方案是将参数的类型更改为 new () => Derived,但我猜这不是你想要的 - 你想要接受 任何Base 继承的非抽象类。

I'm not certain how exactly this works, but I managed to do this by explicitly adding the concrete method signature to the type of the construct signature:

我不确定这个具体是如何工作的,但我设法通过在构造签名的类型中明确添加了具体的方法签名来实现这一点:

function makeSayHelloDerived(DerivedBase: new () => Base & { getName(): string }) {
return class SayHelloDerived extends DerivedBase {
sayHello() {
console.log("hello!");
}
}
}

英文:

The easy solution would be to change the type of the parameter to new () => Derived, but I suppose that's not what you want - you want to accept any non-abstract class that inherits from Base.

I'm not certain how exactly this works, but I managed to do this by explicitly adding the concrete method signature to the type of the construct signature:

function makeSayHelloDerived(DerivedBase: new () => Base & { getName(): string }) {
  return class SayHelloDerived extends DerivedBase {
    sayHello() {
      console.log("hello!");
    }
  }
}

huangapple
  • 本文由 发表于 2023年6月19日 15:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定