Typescript - Overloading private method

10.3k Views Asked by At

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

There are 1 best solutions below

2
On

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?