I currently have a function that needs to accept a class type, instantiate the class and return the INSTANCE, of a template type. I am getting an error: 'ActorType' only refers to a type, but is being used as a value here.
spawnActor<ActorType extends Actor>(actorClass: typeof ActorType): ActorType {
let actorObject = new actorClass();
actorObject.sayHello(); // specific to Actor
return actorObject;
}
Currently I found that the declaration works if I make the parameter type not using the template, actorClass: Actor
rather than actorClass: ActorType
. However when the function is called I have to specify the template type explicitly as it can't be inferred from the class passed into the actorClass
parameter.
spawnActor<ActorType extends Actor>(actorClass: typeof Actor): ActorType {
let actorObject = (new actorClass() as ActorType); // must cast to template type
actorObject.sayHello(); // specific to Actor
return actorObject;
}
let a = spawnActor<MyActorClass>(MyActorClass); // have to give template type for a to have correct type
//let a = spawnActor(MyActorClass); // ideally it would be inferred