Confusing error when returning generic in terms of this
from method called on union of subclasses
#36307
Labels
Fix Available
A PR has been opened for this issue
Needs Investigation
This issue needs a team member to investigate its status.
TypeScript Version: 3.8.0-dev.20200118
Search Terms: union of subclasses with "this" generic
Code
Playground Link
Expected behavior:
a.doThing()
should have typePromise<Bar | Baz>
.result
should have typeBar | Baz
.Actual behavior:
a.doThing()
has the typePromise<Bar> | Promise<Baz>
. The.then
call has an "This expression is not callable" error on it:The same behavior is observable using
Array
and.map
rather thanPromise
and.then
: https://www.typescriptlang.org/play/?ts=3.8.0-dev.20200118&ssl=1&ssc=1&pln=16&pc=1#code/CYUwxgNghgTiAEkoGdnwGIHtPwN4Ch4j5hMAVACwEsA7AcwAoBKALngEEYYoBPAHgAu1ZAD58AX3z5QSOImip4AIVjwQADwEgawNFhwFi8AEaw2NAK4BbYyBgBuCdPDQ5SRSoBeazdt0ZsPEJiU09za1sHJ2dZBAA3VSg2FRh4AB9lKE9HKAA6UkpaRiZcqygABwY4ZAsIAXgAXhEgiSZHIAThis behavior is what I think is appropriate here since
Bar
andBaz
are both subclasses ofFoo
, and therefore thedoThing
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.
The text was updated successfully, but these errors were encountered: