I'm having an issue which defining a generic type based on a type I've passed in.
I have a piece of code witch “activates” a class, I can’t get the type information from the type parameter so I am passing in class object (not an instance). However this breaks the Type inference.
Here is a simplified example of what I'm trying to do:
interface IActivatable {
id: number;
name:string;
}
class ClassA implements IActivatable {
public id: number;
public name: string;
public address:string;
}
class ClassB implements IActivatable {
public id: number;
public name: string;
public age: number;
}
function activator<T extends IActivatable>(type:T): T {
// do stuff to return new instance of T.
}
var classA:ClassA = activator(ClassA);
So far the only solution I’ve been able to come up with is to change the type of the type
argument to any
and manually set the generic type also (as shown below). However this seems long winded, is there another way to achieve this.
function activator<T extends IActivatable>(type:any): T {
// do stuff to return new instance of T.
}
var classA:ClassA = activator<ClassA>(ClassA);
Thanks for any help you can give.