What is the equivalent of protected in TypeScript?

Updates

November 12th, 2014. Version 1.3 of TypeScript is available and includes the protected keyword.

September 26th, 2014. The protected keyword has landed. It is currently pre-release. If you are using a very new version of TypeScript you can now use the protected keyword… the answer below is for older versions of TypeScript. Enjoy.

View the release notes for the protected keyword

class A {
    protected x: string = 'a';
}

class B extends A {
    method() {
        return this.x;
    }
}

Old Answer

TypeScript has only private – not protected and this only means private during compile-time checking.

If you want to access super.property it has to be public.

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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)