TypeScript: `new myVariable.constructor()` doesn't compile

class Test {
    num: number;

    constructor(num: number) {
        this.num = num;
    }
}

var t = new Test(1);
var u = new t.constructor(2);

This code throws a compile error, but appears to produce valid code when entered into http://www.typescriptlang.org/Playground.

EDIT: This code correctly compiles after changing the last line to:

var u = new (<{ new(...args: Array<any>): Test }>t.constructor)(2);

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 16 (13 by maintainers)

Most upvoted comments

Let’s track at #4586