So I have an entity component system, and basically when you add a component object to an entity its binds all the methods to the entity object.
class Component {
constructor(){}
componentMethod() {
console.log('component method called');
}
}
class Entity {
constructor(){}
addComponent(component) {
Object.getOwnProperties(component).forEach(p => {
// some logic make sure its not constructor or duplicate in entity
this[p] = component[p].bind(component);
})
}
}
const component = new Component();
const entity = new Entity();
// works fine
entity.addComponent(component);
entity.componentMethod(); // works if I type entity as any but typescript is throwing an error when I type entity as Entity
Error
Error:() TS2339: Property 'componentMethod' does not exist on type 'Entity'.