Extending vs. implementing a pure abstract class in TypeScript

前端 未结 4 1597
清酒与你
清酒与你 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:48

    The implements keyword treats the A class as an interface, that means C has to implement all the methods defined in A, no matter if they have an implementation or not in A. Also there are no calls to super methods in C.

    extends behaves more like what you'd expect from the keyword. You have to implement only the abstract methods, and super calls are available/generated.

    I guess that in the case of abstract methods it does not make a difference. But you rarely have a class with only abstract methods, if you do it would be much better to just transform it to an interface.

    You can easily see this by looking at the generated code. I made a playground example here.

提交回复
热议问题