Consider an existing JS constructor function
var Foo = function (x,y) {
this.x = x;
this.y = y;
}
var foo = new Foo(1,2);
This is allowed in TypeScript due to a specific exception which allows new to be called on non-constructor functions only if they're void returning. Now if you want to more strongly type this code, but without converting to full on classes yet, we do not allow you to do so in a fully typesafe manner:
interface FooInstance {
x: number;
y: number;
}
interface FooConstructor {
new (x: number, y: number): FooInstance;
}
// this an error, new(x,y)=>FooInstance is not assignable to (x,y)=>void
var Foo: FooConstructor = function (x,y) {
this.x = x;
this.y = y;
}
var foo = new Foo(1,2);
We require you to cast the constructor function to any or Function in order for it to be assignable to the FooConstructor interface.
Given that we've made an exception for void returning functions already in order to accommodate this pattern it doesn't seem crazy to make one more exception for them through the assignability relation in order to complete the desired scenario.
See #2299 for another example from a customer.
Consider an existing JS constructor function
This is allowed in TypeScript due to a specific exception which allows
newto be called on non-constructor functions only if they'revoidreturning. Now if you want to more strongly type this code, but without converting to full on classes yet, we do not allow you to do so in a fully typesafe manner:We require you to cast the constructor function to
anyorFunctionin order for it to be assignable to theFooConstructorinterface.Given that we've made an exception for
voidreturning functions already in order to accommodate this pattern it doesn't seem crazy to make one more exception for them through the assignability relation in order to complete the desired scenario.See #2299 for another example from a customer.