Estendendo interfaces em POO em TypeScript
Interfaces em TypeScript podem herdar
umas das outras. Essa interação é
chamada de extensão de interfaces.
Vamos ver um exemplo. Suponha que temos
uma interface ISize:
interface ISize {
height: number;
width: number;
}
Vamos criar uma interface IStyle que
estende ISize:
interface IStyle extends ISize {
color: string;
}
Agora vamos criar uma classe Figure
que implementa a interface IStyle. Nossa
classe precisará implementar tanto a propriedade
da própria interface quanto a de seu pai.
Vamos fazer isso:
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;
}
}
Vamos verificar o funcionamento:
let fig = new Figure(130, 200, 'green');
console.log(fig);
Crie uma interface IProgrammer com
as propriedades salary e language.
Deixe esta interface herdar de IUser
com as propriedades name, birthday.
Faça uma classe Employee que
implemente IProgrammer.