0

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?

3

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.