danielrvt
danielrvt

Reputation: 10916

TypeScript: Cannot call super method on child class

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...

Upvotes: 2

Views: 7917

Answers (1)

Eneko de la Torre
Eneko de la Torre

Reputation: 155

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

Upvotes: 6

Related Questions