5

Hello I would like to achieve this scenario:

class A implements InterfaceForA
{
    a():void
    {
        this.b();
    }
}

Class B extends A
{
    private b():void
    {
        console.log('Hi');
    }
}

But it throws:

error TS2339: Property 'b' does not exist on type 'A'.

So I updated my class and now it throws:

error TS2415: Class 'B' incorrectly extends base class 'A'. Types have separate declarations of a private property 'b'.

With code:

class A implements InterfaceForA
{
    a():void
    {
        this.b();
    }

    private b():void
    {
        console.log('Hello');
    }
}

Class B extends A
{
    private b():void
    {
        console.log('Hi');
    }
}

In C++, I would set in class A method b() as virtual private, and problem would be solved. In JS it wouldn't be problem at all. How to do it in TypeScript?

1 Answer 1

10

In C++ you would actually set it as protected. Private members are private.

Typescript >= 1.3 supports protected qualifier.

See: What is the equivalent of protected in TypeScript?

Sign up to request clarification or add additional context in comments.

2 Comments

In C++, 'private virtual' differs from protected. It can be overridden, but cannot be called from derived class.
@AntonPilyak Thanks! Good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.