Why does abstract class have to implement all methods from interface? Why does abstract class have to implement all methods from interface? typescript typescript

Why does abstract class have to implement all methods from interface?


You need to re-write all of the members/methods in the interface and add the abstract keyword to them, so in your case:

interface baseInter {    name: string;    test();}abstract class abs implements baseInter {    abstract name: string;    abstract test();}

(code in playground)

There was a suggestion for it: Missing property declaration in abstract class implementing interfaces but it was declined for this reason:

Although, the convenience of not writing the declaration would be nice, the possible confusion/complexity arising from this change would not warrant it. by examine the declaration, it is not clear which members appear on the type, is it all properties, methods, or properties with call signatures; would they be considered abstract? optional?


You can get what you want with a slight trick that defeats the compile-time errors:

interface baseInter {    name : string;    test();}interface abs extends baseInter {}abstract class abs implements baseInter{}

This trick takes advantage of Typescript's Declaration Merging, and was originally presented here and posted on a related SO question here.


Interfaces are used for defining a contract regarding the shape of an object.

Use constructor to pass in properties to the class

interface BaseInter{  name : string;  test(): boolean;}abstract class Abs implements BaseInter{  constructor(public name: string){}  test(): boolean{    throw new Error('Not implemented')  }}