When to use Interface and Model in TypeScript / Angular2

后端 未结 4 810
不知归路
不知归路 2020-11-29 15:19

I recently watched a Tutorial on Angular 2 with TypeScript, but unsure when to use an Interface and when to use a Model to hold data structures.

Example of interface

4条回答
  •  不知归路
    2020-11-29 15:34

    Use Class instead of Interface that is what I discovered after all my research.

    Why? A class alone is less code than a class-plus-interface. (anyway you may require a Class for data model)

    Why? A class can act as an interface (use implements instead of extends).

    Why? An interface-class can be a provider lookup token in Angular dependency injection.

    from Angular Style Guide

    Basically a Class can do all, what an Interface will do. So may never need to use an Interface.

提交回复
热议问题