Код IT
← Каталог

Объекты и классы в TypeScript — `implements` нескольких интерфейсов

Фрагмент из «Объекты и классы в TypeScript»: `implements` нескольких интерфейсов.

TypeScript main.ts
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 });
  }
}