Referencing the constructor function

You can use the constructor type literal or an object type literal with a construct signature to describe the type of a constructor (see, generally, section 3.5 of the language spec). To use your example, the following should work:

interface IGeometry {
    x: number;
    y: number;
}

class GeometryTypeInfo
{        
    constructor (public typeId: number, public typeName: string, public fnCtor: new (...args: any[]) => IGeometry) {
    }
    createInstance(...args: any[]) : IGeometry { return new this.fnCtor(args); }
}

class Point implements IGeometry {
    constructor(public x: number, public y: number) { }

    public static type_info = new GeometryTypeInfo(1, 'POINT', Point);
}

Notice the constructor type literal in GeometryTypeInfo's constructor parameter list, and the new call in the implementation of createInstance.


typeof YourClass gives you constructor type which can be used in type annotations.

YourClass and this.constructor is constructor itself. So, this code compiles:

class A {}

const B : typeof A = A;

this.constructor is not recognized as value of constructor type by TypeScript (which is funny), so in situations like that you need to use some cheating casting it no any

new (<any> this.constructor)()

That's it.

Tags:

Typescript