Inheritance method call triggers Typescript compiler error

后端 未结 2 1722
陌清茗
陌清茗 2020-12-18 00:30

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

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

  constructor(){
    //...
         


        
2条回答
  •  庸人自扰
    2020-12-18 00:50

    You could create an "internal" method that is protected that actually performs the logic. Since you can't call it outside of the class, the this will always be in the correct context.

    export class rootData{
      id:string
      //...
    
      constructor(){
        //...
      }
    
      insert = ():Promise =>{
        return this.insertInternal();
      }
    
      protected insertInternal():Promise{
        //...
      }
    }
    
    class child extends rootData {
      //...   
    
      constructor(){
         super();
      }
    
      protected insertInternal():Promise {
            return super.insertInternal();
        }
    }
    

    You can view a TypeScript Playgound version of it here.

提交回复
热议问题