Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RangeError: Maximum call stack size exceeded – Typescript

When I try to get the name of the Player class in index.ts I get the following error:

/Users/pp/Developer/Typescript/src/lib/player.ts:10
    public get name() { return this.name; }
                                    ^
RangeError: Maximum call stack size exceeded
    at Player.get name [as name]

Here is the code:
lib/player.ts:

export class Player {
    private _name: string;

    constructor(name: string) {
        this._name = name;
    }

    public get name() { return this.name; }
    public set name(value: string) { this.name = value; }
}

index.ts:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { Player } from './lib/player';

const player = new Player('name');
console.log(player.name);

>Solution :

Accessing this.name calls get name() which accesses this.name which calls get name() and so on ad infinitum (or, until you exhaust the call stack).

You probably intended to do:

export class Player {
    private _name: string;
    
    constructor(name: string) {
        this._name = name;
    }
    
    public get name() { return this._name; }
    public set name(value: string) { this._name = value; }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading