Learn TypeScript Help

Implementing Multiple Interfaces

Uma classe pode implementar mais de uma 1 interface:

interface Person { name: string; getDetails(): string; } interface DogOwner { dogName: string; getDogDetails(): string; } class Employee implements Person { constructor(public readonly id: string, public name: string, private dept: string, public city: string) { // no statements required } getDetails() { return `${this.name} works in ${this.dept}`; } } class Customer implements Person, DogOwner { constructor(public readonly id: string, public name: string, public city: string, public creditLimit: number, public dogName ) { // no statements required } getDetails() { return `${this.name} has ${this.creditLimit} limit`; } getDogDetails() { return `${this.name} has a dog named ${this.dogName}`; } } let alice = new Customer("ajones", "Alice Jones", "London", 500, "Fido"); let dogOwners: DogOwner[] = [alice]; dogOwners.forEach(item => console.log(item.getDogDetails())); let data: Person[] = [new Employee("fvega", "Fidel Vega", "Sales", "Paris"), alice]; data.forEach(item => console.log(item.getDetails()));

As interfaces são listadas apos a keyword implement:

class Customer implements Person, DogOwner {}

A classe Customer implementa as interfaces Person e DogOwner que significa que um objeto Personatribuido para uma variavel nomeada alice pode ser adicionada para array tipados para objetos Person e DogOwner

26 November 2025