You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSomeParentClass{privatefoo: string;constructor(foo: string){this.foo=foo;}getFoo(){returnthis.foo;}staticSomeStaticFactory(){returnnewthis('hello world');}}constsomeParentInstance=SomeParentClass.SomeStaticFactory();console.log(someParentInstance.getFoo());classSomeChildClassextendsSomeParentClass{someNewMethod(){return'it works';}}constsomeChildInstance=SomeChildClass.SomeStaticFactory();console.log(someChildInstance.someNewMethod());// Property 'someNewMethod' does not exist on type 'SomeParentClass'.(2339)
π Actual behavior
A subclass which uses a Static method of the parent references the wrong this. In the above example, someChildInstance which is returned from SomeChildClass.SomeStaticFactory(); DOES have someNewMethod, but TypeScript barfs on it.
π Expected behavior
I expect TypeScript to understand that the return type from SomeChildClass.SomeStaticFactory(); is an instance of SomeChildClass and not SomeParentClass
The text was updated successfully, but these errors were encountered:
Just pointing out that code like this is unsound in TypeScript. It's legible to provide a different incompatible constructor in your subclass, which would result in new this('hello world') to result in a runtime error.
classSomeChildClassextendsSomeParentClass{constructor(n: number){super('bla')// Causes: n.toExponential is not a function n.toExponential();}}
Bug Report
π Search Terms
π Version & Regression Information
I can replicate this bug in all versions provided by the typescript playground, including Nightly:
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
A subclass which uses a Static method of the parent references the wrong
this
. In the above example,someChildInstance
which is returned fromSomeChildClass.SomeStaticFactory();
DOES havesomeNewMethod
, but TypeScript barfs on it.π Expected behavior
I expect TypeScript to understand that the return type from
SomeChildClass.SomeStaticFactory();
is an instance ofSomeChildClass
and notSomeParentClass
The text was updated successfully, but these errors were encountered: