Interface implementation — implements, bir nechta interface

~15 daq · TypeScript · OOP

It va Mushuk classlari Hayvon interfaceini amalga oshirishi kerak. extends (abstract class) o'rniga implements — class interface shartnomalarini bajarayotganini bildiradi.


implements

interface Hayvon {
  ism:    string;
  ovoz(): string;
}

class It implements Hayvon {
  ism = "Baxter";
  ovoz() { return "Vov!"; }
}

class Mushuk implements Hayvon {
  ism = "Miti";
  ovoz() { return "Miyov!"; }
}

function ovozBer(h: Hayvon): string {
  return `${h.ism}: ${h.ovoz()}`;
}

ovozBer(new It());    // "Baxter: Vov!"
ovozBer(new Mushuk()); // "Miti: Miyov!"

Bir nechta interface

Class bir vaqtda bir nechta interface ni amalga oshirishi mumkin:

interface Saqlanadigan {
  saqlash(): Promise<void>;
  o_chirish(): Promise<void>;
}

interface Validatsiyalanadigan {
  tekshir(): boolean;
  xatolar(): string[];
}

class Foydalanuvchi implements Saqlanadigan, Validatsiyalanadigan {
  constructor(public ism: string, public email: string) {}

  async saqlash(): Promise<void> { /* DB ga */ }
  async o_chirish(): Promise<void> { /* DB dan */ }

  tekshir(): boolean {
    return this.ism.length > 0 && this.email.includes("@");
  }

  xatolar(): string[] {
    const xatolar: string[] = [];
    if (!this.ism)              xatolar.push("Ism bo'sh");
    if (!this.email.includes("@")) xatolar.push("Email noto'g'ri");
    return xatolar;
  }
}

implements vs extends

extends   → class dan meros (bir classdan)
implements → interface shartnomasi (bir nechta)

class A extends B implements C, D { ... }
abstract class Base { umumiy() { return "base"; } }
interface Qiluvchi { bajar(): void; }

class Voris extends Base implements Qiluvchi {
  bajar(): void { console.log("bajarildi"); }
  // umumiy() — merosdan keladi
}

implements — tur tekshiruvi

implements TypeScript ga "bu class ushbu interfacenga mos" deydi. Metod yoki xususiyat yetishmasa — darhol xato:

interface Printer {
  chiqar(matn: string): void;
  qog_ozSoni: number;
}

class Printer implements Printer {
  qog_ozSoni = 100;
  // chiqar metodi yo'q ❌
  // Class 'Printer' incorrectly implements 'Printer'.
  //   Property 'chiqar' is missing
}

Tez-tez uchraydigan xatolar

implements — runtime ta'siri yo'q:

interface Ishlaydigan { ishlash(): void; }
class A implements Ishlaydigan { ishlash() {} }

const x: unknown = new A();
x instanceof Ishlaydigan; // ❌ — interface runtime da yo'q

// instanceof faqat class bilan
x instanceof A; // ✅

Xulosa

interface A { x(): void; }
interface B { y(): string; }

class MyClass implements A, B {
  x(): void   { console.log("x"); }
  y(): string { return "y"; }
}

// extends + implements birga
abstract class Base { umumiy() {} }
class Child extends Base implements A, B {
  x(): void   { /* ... */ }
  y(): string { return ""; }
}

implements — class interface shartnomalarini bajarishini TypeScript ga bildiradi. Bir nechta interface mumkin. Runtime ta'siri yo'q.


Keyingi dars

45-dars: Mapped types — { [K in keyof T]: ... }