2021年4月7日星期三

Abstract Construct Signatures and instantiation with type hierarchy

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?

1 link here

function greet(ctor: Base) {    const instance = new ctor();    instance.printName();  }    greet(Derived);  greet(Base)  

2 link here

function greet(ctor: typeof Base) {    const instance = new ctor();    instance.printName();  }    greet(Derived);  greet(Base);  

3 link here

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

没有评论:

发表评论