Passing class as parameter causes “is not newable” error

We need something to say typeof(MyClass) to distinguish objects from classes in function signatures.

Anyway, you can actually solve you problem by using constructor type signature.
Considering this class:

class MyClass {
    constructor (private name: string) {
    }
}

To pass that class as a type that you can then instantiate in a function, you actually have to duplicate the class constructor signature like this:

function sample(MyClass: new (name: string) => MyClass) {
    var obj = new MyClass("hello");
}

EDIT :
There is a simple solution found on codeplex:

You have to create an interface for your class like this:

interface IMyClass {
    new (name: string): MyClass;
}

Then, use it in your function signature:

function sample(MyClass: IMyClass) {
    var obj = new MyClass("hello");
}

Leave a Comment