How do you access properties of base class in Typescript?

Alex Vaghin picture Alex Vaghin · Oct 10, 2013 · Viewed 10.9k times · Source

There was a suggestion of using code like this

class A {
    // Setting this to private will cause class B to have a compile error
    public x: string = 'a';
}

class B extends A {
    constructor(){super();}
    method():string {
        return super.x;
    }
}

var b:B = new B();
alert(b.method());

and it even got 9 votes. But when you paste it on the official TS playground http://www.typescriptlang.org/Playground/ it gives you and error.

How to access the x property of A from B?

Answer

Pascalz picture Pascalz · Oct 10, 2013

use this rather than super :

class A {
    // Setting this to private will cause class B to have a compile error
    public x: string = 'a';
}

class B extends A {
    // constructor(){super();}
    method():string {
        return this.x;
    }
}

var b:B = new B();
alert(b.method());