Extending vs. implementing a pure abstract class in TypeScript

前端 未结 4 1583
清酒与你
清酒与你 2020-12-02 12:01

Suppose I have a pure abstract class (that is, an abstract class without any implementation):

abstract class A {
    abstract m(): void;
}

4条回答
  •  不思量自难忘°
    2020-12-02 12:46

    I was led here because I had just been asking myself the same question and while reading the answers it ocurred to me that the choice will also affect the instanceof operator.

    Since an abstract class is an actual value that gets emitted to JS it can be used for runtime checks when a subclass extends it.

    abstract class A {}
    
    class B extends A {}
    
    class C implements A {}
    
    console.log(new B() instanceof A) // true
    console.log(new C() instanceof A) // false
    

提交回复
热议问题