Languages
[Edit]
PL

TypeScript - getter interfejsu

0 points
Created by:
Mariam-Barron
781

Język TypeScript nie pozwala na bezpośrednie dodawanie getterów do interfejsu. W interfejsie można jedynie określić właściwość, którą następnie można zaimplementować jako getter w przedstawiony poniżej sposób.

Przykład gettera interfejsu

interface Square {
    readonly name: string;

    readonly x: number;
    readonly y: number;
}

class LocalSquare implements Square {

    public constructor(private _name: string, 
        private _x: number = 0, private _y: number = 0) {
        // tutaj nic...
    }

    get name() : string {
        return this._name;
    }

    get x(): number {
        return this._x;
    }

    get y(): number {
        return this._y;
    }
}

let a = new LocalSquare('A', 1, 2);

console.log(`${a.name}(${a.x}, ${a.y})`);

Kompletacja i prowadzenie:

$ tsc --target ES5 Script.ts
$ node Script.js

Wynik:

A(1, 2)

Uruchamialny przykład online - tutaj . 

Uwagi:

  • konieczne jest włączenie --targer ES5 parametru obsługi getterów podczas kompilacji (sprawdź pełną listę celów tutaj )
  • readonly słowo kluczowe w interfejsie jest opcjonalne

Referencje

  1. Readonly properties - Microsoft Docs

Alternative titles

  1. TypeScript - funkcja pobierająca interfejs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

TypeScript (PL)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join