Skip to main content

Extending Interfaces in TypeScript

· One min read

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;
}