在TypeScript中定义的类的构造函数
示例的第4行(分配component.constructor)导致编译器抱怨: 类型为“Component”的值上不存在属性“constructor”在TypeScript中定义的类的构造函数,typescript,Typescript,示例的第4行(分配component.constructor)导致编译器抱怨: 类型为“Component”的值上不存在属性“constructor” 获取对对象构造函数的引用的正确方法是什么?我的理解是,JavaScript中的所有对象都有一个.constructor属性,该属性指向用于创建该对象的构造函数…这在类型化代码中非常罕见,因此默认情况下它不包含在对象的定义中。您只需转换为任意: export class Entity { add(component: Component,
获取对对象构造函数的引用的正确方法是什么?我的理解是,JavaScript中的所有对象都有一个.constructor属性,该属性指向用于创建该对象的构造函数…这在类型化代码中非常罕见,因此默认情况下它不包含在
对象的定义中。您只需转换为任意
:
export class Entity {
add(component: Component, componentClass?: { new (): Component;}): Entity {
if (!componentClass) {
componentClass = component.constructor
}
/** sniiiiip **/
}
}
componentClass=(component).constructor;
componentClass = (<any>component).constructor;