Difference between interfaces and classes in Typescript

0 votes

What is the different between Typescript Interfaces and Classes? When do I use a Class? When do I use Interfaces? What are the advantages of them?

I need to create some kind of types for an http-request to my backend server (Doing it with Angular 2), like : },

"fields": {
  "project": {
      "id": "10000"
  },
  "summary": "something's wrong",
  "issuetype": {
      "id": "10000"
  },
  "assignee": {             // not neccesary required
      "name": "homer"
  },
  "reporter": {
      "name": "smithers"
  },
  "priority": {            
      "id": "20000"
  }
}

What should I use for building these models?

Jun 14 in TypeSript by Logan
• 1,580 points
7 views

1 answer to this question.

0 votes

Interfaces

  • Describe how an object should look
  • Exists compile time only, with the sole purpose of type checking

Classes

  • Used as a blueprint from which to instantiate/produce objects
  • Can implement an interface which means it has to implement at least all of the properties/methods from the interface

Example:

interface Person {
    name: string;
    id: number;
    doStuff: () => void;
}

// implements Person says: You have to at least implement these things
// which are located on the person interface 
class employee implements Person {
    constructor(public name: string, public id: number){}

    doStuff () {console.log('Doing stuff')}
}

// interfaces can also describe variables and parameters
const p1: Person = {
    name: 'foo',
    id: 34,
    doStuff () {console.log('Doing stuff')}
}
answered Jun 15 by Nina
• 2,900 points

Related Questions In TypeSript

0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31 in TypeSript by Nina
• 2,900 points
32 views
0 votes
1 answer

How to install and run Typescript locally in npm?

It took me a while to figure ...READ MORE

answered Jun 8 in TypeSript by Nina
• 2,900 points
29 views
0 votes
1 answer

Mixing JavaScript and TypeScript in Node.js

Combine the following TypeScript compiler options --allowJs Explicitly supports ...READ MORE

answered Jun 15 in TypeSript by Nina
• 2,900 points
6 views
0 votes
1 answer

Interface type check with Typescript

You can achieve what you want without ...READ MORE

answered May 31 in TypeSript by Nina
• 2,900 points
34 views
0 votes
1 answer

How to apply zoom animation for each element of a list in angular?

Hey @Sid, do check if this link ...READ MORE

answered Jul 30, 2019 in Others by Vardhan
• 13,200 points
469 views
0 votes
1 answer

Can't bind to 'formGroup' since it isn't a known property of 'form'

In order to rectify this error, you ...READ MORE

answered Feb 10 in Others by Rahul
• 8,980 points
391 views
0 votes
1 answer
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 2,900 points
7 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP