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?

Mel Pacheco
  • 769
  • 1
  • 12
  • 26
  • https://stackoverflow.com/questions/14345485/whats-the-difference-between-declare-class-and-interface-in-typescript – LarsMonty Aug 18 '17 at 18:50
  • thank you, this is what I was looking for! – Mel Pacheco Aug 18 '17 at 18:52
  • 4
    Possible duplicate of [Whats the difference between "declare class" and "interface" in TypeScript](https://stackoverflow.com/questions/14345485/whats-the-difference-between-declare-class-and-interface-in-typescript) – Aravind Aug 18 '17 at 18:53

1 Answers1

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.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
FAISAL
  • 33,618
  • 10
  • 97
  • 105