Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ā¤ šŸ’»
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

TypeScript - interface getter

0 contributions
14 points

In TypeScript does not allow to add getters directly to interface.Ā It is possible to specyfy only property in interface that can be implemenetedĀ asĀ getterĀ in following way.

Interface getter example

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) {
        // nothing here...

    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.x}, ${a.y})`);

Complilation and running:

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


A(1, 2)

Run it online here.Ā 


  • to enable getters supportĀ --targer ES5Ā parameter during complilation is necessary (check full list of targets here)
  • readonly keyword in interface is optional


  1. Readonly properties - Microsoft Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more