Are you allowed to specify return types for a class constructor?

Topics: General
Nov 15, 2012 at 10:13 PM
Edited Nov 15, 2012 at 10:15 PM

Is there a correct syntax to specify a return type for a ctor?

In this simple example I can't seem to get that working:

 

 

class Test { 
  constructor(): string { 
  }
}

 

 

Gives a compiler error,  Expected "{"

And

 

class Test {
	constructor() => Test {
		
	}
}

 

Causes the playground to throw errors:

 

SKIPPING refresh because models are weird editor.main.js:10
Object {message: "RangeError: Maximum call stack size exceeded"} editor.main.js:10

 

Interfaces are allowed to define a constructor with a return type:

 

interface MyInterface {
    new () : any;
}

 

How would you implement this interface correctly?

Coordinator
Nov 15, 2012 at 11:03 PM

Answered back on StackOverflow: http://stackoverflow.com/questions/13407036/how-does-typescript-interfaces-with-construct-signatures-work