Access.constructor class defined in TypeScript

export class Entity { add(component: Component, componentClass?: { new (): Component;}): Entity { if (!componentClass) { componentClass = component.constructor } /** sniiiiip **/ } } 

Line 4 of the example (assignment of the .constructor component) makes the compiler complain that:

The constructor property does not exist with a value of type Component

What is the correct way to get a reference to an object constructor? I understand that all objects in JavaScript have a .constructor property that points to the constructor used to create this object ...

+6
source share
1 answer

This is rare enough in typed code, which it did not include in the Object definition by default. Instead, you can simply press any :

 componentClass = (<any>component).constructor; 
+5
source

All Articles