typescript - 'XXX' 没有提供签名 'new (): XXX' 的匹配项

标签 typescript

我有以下类,但在 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;

ComponentComponentType<T>没有导致错误的相同类型。

关于typescript - 'XXX' 没有提供签名 'new (): XXX' 的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42424559/

相关文章:

Javascript 箭头函数代替 for...of

reactjs - 尝试创建可共享的 React 组件 - 但未能导入它

angular - 即使在全局对象上定义,Angular2 上的 onYouTubeIframeAPIReady 也找不到名称 'YT'

typescript - 是否有用于语音识别的 TypeScript 库?

javascript - 如何使用angular8为循环项目的电子邮件和传真显示每一行的验证错误

typescript - TS - 如何对泛型类型内的类型值执行字符串函数

reactjs - 何时使用 React.PropsWithChildren

Typescript 类型定义与推理

node.js - React-geosuggest问题模块未找到: Error: Can't resolve 'prop-types'

angular - Typescript 中全局变量的解构赋值