Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing the constructor function

Tags:

typescript

Im wondering how I can get a reference to a types constructor to pass the function as a value. Basically, I would like to have a generic type registry that would allow instances to be created by calling a member function of a generic type registry instance.

For example:

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

Later:

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

    public static type_info = new GeometryTypeInfo(1, 'POINT', Point); // <- fails
    // also fails: 
    //    new GeometryTypeInfo(1, 'POINT', new Point);
    //    new GeometryTypeInfo(1, 'POINT', Point.prototype);
    //    new GeometryTypeInfo(1, 'POINT', Point.bind(this));
}

Anyone know if it is possible to reference a classes constructor function?

like image 448
Jake H Avatar asked Oct 08 '12 18:10

Jake H


People also ask

What is constructor reference?

Constructor Reference is used to refer to a constructor without instantiating the named class. The Constructor reference mechanism is yet another game changing addition by Java 8. You can pass references to constructors as arguments or assign to a target type.

Which constructor uses reference?

A copy constructor defines what copying means,So if we pass an object only (we will be passing the copy of that object) but to create the copy we will need a copy constructor, Hence it leads to infinite recursion. So, A copy constructor must have a reference as an argument. Highly active question.

Can constructor have function?

In the above example, function Person() is an object constructor function. To create an object from a constructor function, we use the new keyword. Note: It is considered a good practice to capitalize the first letter of your constructor function.

What are the rules for writing constructor function?

Rules to be rememberedA constructor does not have return type. The name of the constructor is same as the name of the class. A constructor cannot be abstract, final, static and Synchronized. You can use the access specifiers public, protected & private with constructors.


1 Answers

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.

like image 139
Brian Terlson Avatar answered Oct 10 '22 20:10

Brian Terlson