getComponent() doesn't accept Abstract Components

Hello,
I have created an abstract class (lets class this AbstractComponent) which extends the Component class. I then extend the AbstractComponent for creating quite a number of custom component classes.

abstract class AbstractComponent extends Component { … }
class ObstacleTypeA extends AbstractComponent { … }
class ObstacleTypeB extends AbstractComponent { … }

The problem is whenever I try to specify the AbstractComponent in the getComponent parameter I get TS error “Cannot assign an abstract constructor type to a non-abstract constructor type.”

I went through the engine code and found that the Class passed over as parameter to the getComponent function weren’t being newd. So I don’t see the reason why only Concrete class constructors need to be passed as the parameter here.

Also one thing I noticed is that if I ignore the TS error and run the game with

this.getComponent(AbstractComponent); // works

I believe the reason for it working is that internally there is a comparison being done on the constructors of the components and it returns the correct component.

So my request would be to allow for abstract component class to be passed as a parameter to the getComponent functions as it doesn’t necessarily invoke the new call on it.

Typescript v4.2 allows for this using the abstract keyword in the constructor signature

type AbstractConstructor = abstract new (…args: any[]) => T

Please have a look and share your thoughts on this.

.

3 Likes