Mel Pacheco
Mel Pacheco

Reputation: 789

typescript models from classes, why?

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

Answers (1)

FAISAL
FAISAL

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

Related Questions