继承方法调用触发Typescript编译器错误 [英] Inheritance method call triggers Typescript compiler error

查看:76
本文介绍了继承方法调用触发Typescript编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用webstorm打字稿编译器时遇到问题.我有以下课程

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();
    }
}

因此,键入"super",我会在智能感知中看到所有rootData公共方法.但是在设置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:只能通过'super'关键字访问基类的公共方法和受保护方法

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

在TS游乐场中尝试过,它正在运行(认为是简化版).

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

感谢您的帮助.

在检查了已编译的javascript之后,将在其中调用super方法.因此,编译器给出了一个错误,但是可以编译...

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

推荐答案

由于 super 调用被重定向到 prototype ,因此您不能使用 property ,并且需要使用方法,即不能使用 =()=> .

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

固定代码:

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

  constructor(){
    //...
  }

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

class child extends rootData {
  //...   

  constructor(){
     super();
  }

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

这篇关于继承方法调用触发Typescript编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆