classes vs interfaces in Angular(TypeScript)

2019-09-21 13:30发布

问题:

What are the pros and cons of using classes vs interfaces in angular ?what is the best practice and when to use classes and when to use interfaces in angular?

回答1:

I guess, this part from the Angular Style Guide says it all:

Consider using a class instead of an interface for services and declarables (components, directives, and pipes).

Consider using an interface for data models.



回答2:

I'll try to give you the short answer, that should give you the overall picture by demonstrating the key differences:

Interfaces are not compiled to javascript.

vs

Classes are compiled to javascript classes.


Interfaces are used for tooling (Intellisense/autocomplete) only.

vs

Classes are part of runtime program logic.