Yoann Yoann - 5 months ago 24
Node.js Question

Inheritance method call triggers Typescript compiler error

I am having an issue with webstorm typescript compiler. I have the following classes

export class rootData{
id:string
//...

constructor(){
//...
}

insert = ():Promise<any> =>{
//...
}
}

class child extends rootData {
//...

constructor(){
super();
}

insert = ():Promise<any> => {
return super.insert();
}
}


So typing "super", I see all rootData public methods in the intellisense. But after setting super.insert(), I get the following error :

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

Tried in TS playground, it is working (simplified version thought).

Thanks for your help.

EDIT: After checking the compiled javascript, the call of the super method is there. So the compiler gives an error but compiles...

Answer

Because super calls are redirected to the prototype you cannot use a property and need to use a method i.e. can't use = ()=>.

Fixed code:

export class rootData{
  id:string
  //...

  constructor(){
    //...
  }

  insert():Promise<any>{
    //...
  }
}

class child extends rootData {
  //...   

  constructor(){
     super();
  }

  insert():Promise<any> {
        return super.insert();
    }
}