The new Operator

javascript, typescript

Solution

You should be able to do:

function foo(){
    return new String("something");
} 
var x = new foo(); 

console.log(x);

You can return any object, but literals don't work. See here: What values can a constructor return to avoid returning this?

Problem

Is it possible to write a javascript function that follows this (valid) typescript interface: ``` interface Foo{ // constructor: new (): string; } ``` i.e. Something that when called with a new operator returns a string. e.g. the following will not work. ``` function foo(){ return "something"; } var x = new foo(); // x is now foo (and not string) whether you like it or not :) ```

Original source

Related problems