Maximus Maximus - 1 month ago 6
TypeScript Question

Why use abstract class instead of interface if no methods are implemented

Angular2 in its file

core/src/metadata/lifecycle_hooks.ts
declares class as abstract

export abstract class OnInit { abstract ngOnInit(): void; }


why not declare it as interface since it doesn't have implementation?

Answer

Interfaces don't exist in java-script; So, presumably, using abstract classes (that don't have any member implementations) is a useful way to have that "contract" have a real type at run time. One possible use would be for dependency injection.