2

When I try to override a parent method and I use super inside, I get this error:

error TS2340: Only public and protected methods of the base class are accessible via the 'super' keyword.

    return super.toJson(["password", ...blacklist]);

This is an example:

abstract class BaseUser {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    toJson = () => {
        return Object.assign({}, this);
    }
}

class MyUser {
   ...
   toJson = (blacklist) => {
       ...
       const obj = super.toJson();
       ... 
   }
}

Don't know what I'm doing wrong...

3
  • 3
    MyUser does extend BaseUser in you code. Even if it did, toJson is not a method it is a field that is a function and thus not present on the prototype (it is assigned in the constructor) and you can't call it through super. Make it a regular method and it should work Commented Mar 29, 2019 at 17:14
  • I was going to post this as an answer but your comment says it all so I let you post it as an answer if you want to. Commented Mar 29, 2019 at 20:59
  • Possible duplicate of Inheritance method call triggers Typescript compiler error Commented Apr 3, 2019 at 11:44

1 Answer 1

6

As @Titian Cernicova-Dragomir said in the comments, you should use method instead. You can check these answers:

Inheritance method call triggers Typescript compiler error

Error when performing Inheritance in TypeScript

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

Comments

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.