I am new to TypeScript and this is the function I've written:
/**
* @function getComponent()
* @description finds and returns the requested type of component, null if not found
* @return {any} component
*/
public getComponent<T extends Component>(): T{
for(let i = 0; i < this.componentList.length; i++){
if(<T>this.componentList[i] instanceof Component){
return this.componentList[i] as T;
}
}
return null;
}
This function is inside the GameObject class which contains a list of Component objects
Component is an abstract function that can be extended by other classes. I want this function to return the type of Component requested by the user. For example something like this:
let gameObject = new GameObject();
let audioComponent = gameObject.getComponent<AudioComponent>(); // no syntax error because AudioComponent extends Component
let myComponent = gameObject.getComponent<foo>(); // syntax error, foo doesn't extend Component
I believe I'm doing something wrong, any help?