19

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.

Antony Jones
  • 561
  • 1
  • 4
  • 15

4 Answers4

51

According to the language specification, you need to refer to the class type by it's constructor function. So instead of using type:T, use type: { new(): T;} as follows:

function activator<T extends IActivatable>(type: { new(): T ;} ): T {
    // do stuff to return new instance of T.
    return new type();
}

var classA: ClassA = activator(ClassA);
user229044
  • 232,980
  • 40
  • 330
  • 338
blorkfish
  • 21,800
  • 4
  • 33
  • 24
  • 4
    This is so lame :( I mean how typescript handles this. Your answer is good. – Serge Profafilecebook Nov 08 '15 at 12:51
  • 13
    Somewhat late (but useful) comment - if your constructor takes args then the `new()` needs to as well - or a more generic: `type: {new(...args : any[]): T ;}` – Rycochet Apr 14 '16 at 08:47
  • 4
    If someone implements the IActivatable interface with a constructor that has several arguments how do you infer the arguments ? Because using `...args: any[]` isn't typesafe. – dll Oct 11 '18 at 21:22
  • 2
    `(type: { new(): T ;} )` can be more simply written as `(type: new() => T)` – Sanjay Verma Sep 05 '21 at 00:42
  • @blorkfish Any pointer to the TypeScript language specification where it is mentioned this way? – Pradip Mar 17 '23 at 08:15
14

This is how the Typescript team recommends doing it:
Basically the same as blorkfish's answer, but extracted to an interface.

interface IConstructor<T> {
    new (...args: any[]): T;

    // Or enforce default constructor
    // new (): T;
}

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: IConstructor<T>): T {
    return new type();
}

const classA = activator(ClassA);

Source

Red Riding Hood
  • 1,932
  • 1
  • 17
  • 36
5

The type information in TypeScript is all erased during compilation, so you can't directly use any of the generic types, for example, at runtime.

So here is what you can do...

You can create classes by name by passing the name as a string. Yes; this involves waving your magic-string-wand. You also need to be aware of anything in your toolchain that may affect the names, for example any minifier that will crush the names (resulting in your magic string being out of sync):

class InstanceLoader<T> {
    constructor(private context: Object) {

    }

    getInstance(name: string, ...args: any[]) : T {
        var instance = Object.create(this.context[name].prototype);
        instance.constructor.apply(instance, args);
        return <T> instance;
    }
}

var loader = new InstanceLoader<IActivatable>(window);

var example = loader.getInstance('ClassA');

You can also get type names from instances at runtime, which I have shown in example format below taken from Obtaining TypeScript Class Names at Runtime:

class Describer {
    static getName(inputClass) { 
        var funcNameRegex = /function (.{1,})\(/;
        var results = (funcNameRegex).exec((<any> inputClass).constructor.toString());
        return (results && results.length > 1) ? results[1] : "";
    }
}

class Example {
}

class AnotherClass extends Example {
}

var x = new Example();
var y = new AnotherClass();

alert(Describer.getName(x)); // Example
alert(Describer.getName(y)); // AnotherClass

This would only be relevant if you wanted to generate "another of the same kind" as you could grab the type name and then use the object create stuff to get another.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    That is essentially the conclusion I came to also (difference is I was passing the constructor function, and you pass the name). It’s a shame there isn't better construct for it but that’s life I guess. Thanks for the confirmation. – Antony Jones Jul 11 '14 at 07:53
  • This can now be done much easier. Use blorkfish's or my answer below. – Red Riding Hood Jan 09 '17 at 15:43
0

Your problem is that ClassA is not actually of type T extends IActivatable. In fact, ClassA in the compiled javascript is actually a function that returns a constructor function.

Peter
  • 1,032
  • 1
  • 11
  • 26
  • What I’m asking is, is there a way to set the generic type as well as passing in the function without specifying the `ClassA` multiple times. – Antony Jones Jul 10 '14 at 14:14