Extending Interfaces in OOP in TypeScript
Interfaces in TypeScript can inherit from each other. This interaction is called extending interfaces. Let's look at an example. Let's say we have an interface ISize:
interface ISize {
height: number;
width: number;
}
Let's create an interface IStyle that will extend ISize:
interface IStyle extends ISize {
color: string;
}
Now let's create a class Figure that implements the interface IStyle. Our class will need to implement both the interface's property and its parent's. Let's do this:
class Figure implements IStyle {
height: number;
width: number;
color: string;
constructor(height: number, width: number, color: string) {
this.height = height;
this.width = width;
this.color = color;
}
}
Let's check the work:
let fig = new Figure(130, 200, 'green');
console.log(fig);
Create an interface IProgrammer with properties salary and language. Let this interface inherit IUser with properties name, birthday.
Make a class Employee that implements IProgrammer.