Given the code below:
abstract class Base { abstract getName(): string; printName() { console.log("a") } } class Derived extends Base { getName() { return ""; } }
There is at least one error for each of three scenarios below. But I am able to run it. Why is it?
What functionality of the code below is? Is it a correct way?
function greet(ctor: Base) { const instance = new ctor(); instance.printName(); } greet(Derived); greet(Base)
function greet(ctor: typeof Base) { const instance = new ctor(); instance.printName(); } greet(Derived); greet(Base);
function greet(ctor: new () => Base) { const instance = new ctor(); instance.printName(); } greet(Derived); greet(Base);
https://www.typescriptlang.org/docs/handbook/2/classes.html#abstract-construct-signatures
https://stackoverflow.com/questions/66993587/abstract-construct-signatures-and-instantiation-with-type-hierarchy April 08, 2021 at 04:19AM
没有评论:
发表评论