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
{{ message }}
Repository navigation
Confusing error when returning generic in terms of this from method called on union of subclasses #36307
Expected behavior: a.doThing() should have type Promise<Bar | Baz>. result should have type Bar | Baz.
Actual behavior: a.doThing() has the type Promise<Bar> | Promise<Baz>. The .then call has an "This expression is not callable" error on it:
This expression is not callable.
Each member of the union type '(<TResult1 = Bar, TResult2 = never>(onfulfilled?: ((value: Bar) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => Promise<...>) | (<TResult1 = Baz, TResult2 = never>(onfulfilled?: ((value: Baz) => TResult1 | PromiseLike<...>) |...' has signatures, but none of those signatures are compatible with each other.(2349)
This behavior is what I think is appropriate here since Bar and Baz are both subclasses of Foo, and therefore the doThing function being called is the same for both types in the union. I can see that outside of this specific situation, calling a function on a union type should return the union of the possible return types. However, I think it would be better if the behavior could be different for subclasses somehow.
If not, what sort of type assertion could I use to get around this?
Related Issues:#35953 seems kinda similar, but I can't make much sense of the example given there myself.
changed the title [-]Confusing `this` semantics with generics and subclass unions[/-][+]Confusing `this` semantics with generics, subclass unions, and promises[/+]on Jan 19, 2020
changed the title [-]Confusing `this` semantics with generics, subclass unions, and promises[/-][+]Confusing error when returning generic in terms of `this` from method called on union of subclasses[/+]on Jan 19, 2020
Activity
[-]Confusing `this` semantics with generics and subclass unions[/-][+]Confusing `this` semantics with generics, subclass unions, and promises[/+][-]Confusing `this` semantics with generics, subclass unions, and promises[/-][+]Confusing error when returning generic in terms of `this` from method called on union of subclasses[/+]Convert all Message<this> to Message for now
RyanCavanaugh commentedon Feb 26, 2020
Wesley Wigham (@weswigham) am I correct that #31023 would solve this?
weswigham commentedon Feb 26, 2020
I believe so, yes.