← Каталог
Объекты и классы в TypeScript — `implements` нескольких интерфейсов
Фрагмент из «Объекты и классы в TypeScript»: `implements` нескольких интерфейсов.
interface Serializable {
toJSON(): string;
}
interface Timestamped {
updatedAt: Date;
}
class Article implements Serializable, Timestamped {
constructor(
public title: string,
public updatedAt: Date,
) {}
toJSON(): string {
return JSON.stringify({ title: this.title, updatedAt: this.updatedAt });
}
} interface Serializable {
toJSON(): string;
}
interface Timestamped {
updatedAt: Date;
}
class Article implements Serializable, Timestamped {
constructor(
public title: string,
public updatedAt: Date,
) {}
toJSON(): string {
return JSON.stringify({ title: this.title, updatedAt: this.updatedAt });
}
}