最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Class 'B' incorrectly extends base class 'A' (two private methods named the same) -

programmeradmin7浏览0评论

I am getting the error

Class 'B' incorrectly extends base class 'A'.
Types have separate declarations of a private property 'method'.

abstract class A {
  private method() {}
}

abstract class B extends A {
  private method() {}
}

When the method in class A is commented out, then the error goes away. What can I do to have two private methods that are named the same?

I am getting the error

Class 'B' incorrectly extends base class 'A'.
Types have separate declarations of a private property 'method'.

abstract class A {
  private method() {}
}

abstract class B extends A {
  private method() {}
}

When the method in class A is commented out, then the error goes away. What can I do to have two private methods that are named the same?

Share Improve this question edited Feb 16, 2018 at 15:28 Get Off My Lawn asked Feb 16, 2018 at 15:20 Get Off My LawnGet Off My Lawn 36.3k46 gold badges197 silver badges372 bronze badges 6
  • Naming them differently would work. – Feathercrown Commented Feb 16, 2018 at 15:21
  • I thought that this was acceptable. It is valid if the two are public. – Get Off My Lawn Commented Feb 16, 2018 at 15:24
  • That's the problem using just typescript, convert it into plain JavaScript and you can follow the code along how it ends up the way it does, – Nope Commented Feb 16, 2018 at 15:26
  • Can you convert A into an interface, and move the existing A.method into a new abstract class that implements A? – earldouglas Commented Feb 16, 2018 at 15:27
  • What can I do to have two private methods that are named the same? - When extending one class from another you cannot have two private methods named the same as explained in this - Possible duplicate of typescript derived class cannot have the same variable name? – Nope Commented Feb 16, 2018 at 15:32
 |  Show 1 more comment

2 Answers 2

Reset to default 16

Since Typescript sits on top of Javscript, and Javascript does not allow inherited classes to have different private implementations for a function (all functions will end up on the prototype of the class) it makes sense that Typescript would not allow you to do this.

If this were allowed you would be basically be overriding the private member method and since it is marked as private it was probably not intended for public consumption/overriding.

You could override the function if you declared it as public/protected

abstract class A {
    protected method() { }
}

abstract class B extends A {
    protected method() { }
}

Or if you really want to override a private method, you could do the following, although private methods are marked as private for a reason and it may be best to avoid this:

class B extends A {

}
(B.prototype as any)['method'] = function(this: B) {
}

It's all very strange, but if you really need to, try this:

class Foo {
    private go() {
    }
}

class Bar extends Foo {
    constructor() {
        super();
        this["go"] = this.foo;
    }

    private foo() {

    }
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论