尝试将一个组件导入到第二个组件时,我收到以下错误。
“InputComponent”类型中缺少属性“prototype”,但“typeof InputComponent”类型中需要属性“prototype”。TS(2741)
测试.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();
}
}Run Code Online (Sandbox Code Playgroud)
这是错误和沙箱的屏幕截图。
小智 7
问题是您指的是public inputtotypeof InputComponent但您试图将 a 分配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();
}
}Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19938 次 |
| 最近记录: |