【问题标题】:Typescript Class Import Error: Property 'prototype' is missing in type '<component name>' but required in type 'typeof <component name>'. TS(2741)Typescript 类导入错误:“<component name>”类型中缺少属性“prototype”,但“typeof <component name>”类型中需要。 TS(2741)
【发布时间】:2020-07-26 06:34:36
【问题描述】:

试图将一个组件导入到第二个组件我收到以下错误。

“InputComponent”类型中缺少属性“原型”,但这是必需的 在类型'typeof InputComponent'中。 TS(2741)

test.ts

class InputComponent {

  add():number{
    return 1 + 1
  }
}

class Example {
  public input: typeof InputComponent;

  constructor(Input: typeof InputComponent) {
    this.input = new Input();
  }
}

class Example2 {
  public input: typeof InputComponent;

  constructor(Input: typeof InputComponent = InputComponent) {
    this.input = new Input();
  }
}

这是错误和沙箱的屏幕截图。

typescript sandbox

【问题讨论】:

    标签: typescript


    【解决方案1】:

    问题是您将public input 引用到typeof InputComponent,但您尝试为其分配new InputComponent()

    将其更改为public input: InputComponent 即可解决问题。

    在打字稿中InputComponent 指的是类的一个对象,而typeof InputComponent 指的是类本身。

    class InputComponent {
    
      add():number{
        return 1 + 1
      }
    }
    
    class Example {
      public input: InputComponent;
    
      constructor(Input: typeof InputComponent) {
        this.input = new Input();
      }
    }
    
    class Example2 {
      public input: InputComponent;
    
      constructor(Input: typeof InputComponent = InputComponent) {
        this.input = new Input();
      }
    }

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2021-01-05
      • 2017-11-10
      • 2022-01-19
      相关资源
      最近更新 更多