When to use Interface and Model in TypeScript / Angular2

后端 未结 4 809
不知归路
不知归路 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:31

    The Interface describes either a contract for a class or a new type. It is a pure Typescript element, so it doesn't affect Javascript.

    A model, and namely a class, is an actual JS function which is being used to generate new objects.

    I want to load JSON data from a URL and bind to the Interface/Model.

    Go for a model, otherwise it will still be JSON in your Javascript.

提交回复
热议问题