Backbencher.dev

Extending Interfaces in TypeScript

Last updated on 6 Feb, 2023

We can implement inheritance in interfaces.

Say we have an interface for Person:

interface Person {
  name: string;
}

Also, we have another interface for Teacher:

interface Teacher {
  subject: string;
}

Now if we have a class Master that needs to implement both Person and Teacher, we can do like this:

class Master implements Person, Teacher {
  name;
  subject;
}

Above way is one technique.

There is another way. We know that a Teacher interface always needs to have a name also. Therefore, we can extend Teacher from Person.

interface Teacher extends Person {
  subject: string;
}

Once Teacher extends Person, we need to implement only Teacher.

class Master implements Teacher {
  name;
  subject;
}
--- ○ ---
Joby Joseph
Web Architect