Reputation: 789
I'm new to typescript, I'm using it to build angular apps. sometimes I see models like this
export interface Item {
name: string
}
and sometimes I see this
export class Lesson {
constructor(
public $key:string)
}
with static methods like
static fromJson({$key}) {}
whats the benefit?
Upvotes: 0
Views: 506
Reputation: 34673
interface
allows you to create a model with properties only.
However, class
gives you the advantage to define and use functions.
E.g. if you want to initialize an object, with an interface you have to initialize all the properties, whereas with a class you can do the same with the class constructor
.
Its really up to the user what they want to use. In my opinion, a model using class gives more flexibility as compared to the interface.
Upvotes: 3