export class Entity {
add(component: Component, componentClass?: { new (): Component;}): Entity {
if (!componentClass) {
componentClass = component.constructor
}
/** sniiiiip **/
}
}
Line 4 of the example (assigning component.constructor) causes the compiler to complain that:
The property 'constructor' does not exist on value of type 'Component'
What's the proper way to get a reference to an objects constructor? My understanding is that all Objects in JavaScript have a .constructor property that points to the constructor used to create that object...
This is rare enough in typed code that it's not included by default on the definition of Object
. You can simply cast to any
instead:
componentClass = (<any>component).constructor;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With