My question pertains to a typing issue in TypeScript.
Consider the following code snippet:
class Foo {
static classFoo() {
return new this();
}
}
class Bar extends Foo {
instanceBar() {}
}
Bar.classFoo().instanceBar();
When running this code, an error occurs:
The property 'instanceBar' does not exist on type 'Foo'
However, it is important to note that this error is misleading because when Bar.classFoo()
is called, this === Bar
. This means that, despite the type error, the code functions properly due to how inheritance works in ES6 classes.
So why does this error occur? Is it a known bug within TypeScript? And most importantly, what is the solution for fixing this issue?