我有以下类,但在 for 循环中出现此错误:
[ts] Argument of type 'Component' is not assignable to parameter of type 'ComponentType'. Type 'Component' provides no match for the signature 'new (): Component'
这个类看起来像这样:
class Prefab {
public components: Component[] = [];
public static make(object: Prefab): GameObject {
let go = new GameObject;
for (let comp of object.components) {
// The error is here:
go.addComponent(comp);
}
return go;
}
}
然后我的组件类就有了这个接口(interface):
interface ComponentType<T extends Component> {
new (): T;
}
下面是addComponent
的定义:
addComponent<T extends Component>(type: ComponentType<T>): T;
我可以在这里做什么?
最佳答案
在for
循环,comp变量指的是Component
类型。
for (let comp of object.components) {
而你的 addComponent
方法需要 ComponentType<T>
类型
addComponent<T extends Component>(type: ComponentType<T>): T;
Component
和 ComponentType<T>
没有导致错误的相同类型。
关于typescript - 'XXX' 没有提供签名 'new (): XXX' 的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42424559/