Search code examples
typescript

Passing class as parameter causes "is not newable" error


I'm trying to pass a class as a parameter to some function, that will instantiate this class and return it. Here is my code:

module A.Views {
  export class View { ... }
}

module A.App {
  export class MyApp {
    ...
    registerView(viewKlass:A.Views.View):void
    {
        var test = new viewKlass;
    } 
  }
}

When i'm trying to compile this, i'm getting:

(...): Value of type 'Views.View' is not newable.

What am I doing wrong?

If a newable type value is an object constructor how do i pass the constructor function at runtime?


Solution

  • 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");
    }