Assuming that the setting noImplicityAny
is enabled.
Consider the following:
function Bar() {
this.f = 100;
}
The following code snippet will not function as expected:
let x = new Bar();
An error message will be displayed:
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
While it may seem understandable, adding an interface changes things:
interface Bar {
f: number;
}
function Bar() {
this.f = 100;
}
let x = new Bar();
console.log(x.f);
When inspecting the type of Bar
, you'll notice:
interface Bar function Bar(): void
In this case, there seems to be some merging occurring. The reason for this change in behavior remains uncertain. Why does this merged type work now and why is the implied return type no longer any
?