TypeScript Syntax for Custom Getters and Setters

Posted 11/11/2016 3:56 PM by Corey Klass

Here is the TypeScript syntax to define custom getters and setters for variables:

    private _myVar: number

    get myVar(): number {
        return this._myVar;
    }

    set myVar(myVar) {
        this._myVar = myVar;

        callSomeOtherFunction();
    }


    retrieveCurrentMyVar(): number {
        return this.myVar; // calls the getter myVar() above
    }