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
.call on an union with different arguments counts fails when passing any #58468
Before version 4.5 both the .calls in the example below failed. Since then, .call(null, something-not-any) is accepted and .call(null, something-any) failes.
So in this case we end up with multiple inference candidates. We have:
covariant: [any]
contravariant: [] and [a: number]
When multiple inference candidates are present, TS tries to select the best match based on different criteria. Since contravariant candidates don't overlap sufficiently enough the first one gets picked as overall inferredContravariantType. Not: This is order-dependent so when you switch the order of ur union members it magically works because the other one gets picked there.
Since we have both inferredCovariantType and inferredContravariantType we also need to pick between those 2. It happens so that preferCovariantType gets computed as false in your case. It's rejected based on this check:
[any] is not a subtype of either of the contravariant candidates, it's not a subtype of [] and it's not a subtype of [a: number]. So it ends up picking the covariant candidate and it fails to typecheck ur arguments against inferred signature further down the line.
So it turns out one of my PR fixes this case alrady: #57909
But what happens in the implementation here got me thinking... why do we even have multiple contravariant candidates here? we do have that because the inference infer from each of your signatures separately here. This is kinda a single call though. TS is able to combine union (or intersection) signatures into one. Shouldn't we then infer from that combined signature here?
So that led me to creating this experiment that also addresses this issue: #58482
Activity
Andarist commentedon May 8, 2024
I love fun issues. Expect me digging into this one later 😉
Andarist commentedon May 9, 2024
So in this case we end up with multiple inference candidates. We have:
[any][]and[a: number]When multiple inference candidates are present, TS tries to select the best match based on different criteria. Since contravariant candidates don't overlap sufficiently enough the first one gets picked as overall
inferredContravariantType. Not: This is order-dependent so when you switch the order of ur union members it magically works because the other one gets picked there.Since we have both
inferredCovariantTypeandinferredContravariantTypewe also need to pick between those 2. It happens so thatpreferCovariantTypegets computed asfalsein your case. It's rejected based on this check:[any]is not a subtype of either of the contravariant candidates, it's not a subtype of[]and it's not a subtype of[a: number]. So it ends up picking the covariant candidate and it fails to typecheck ur arguments against inferred signature further down the line.So it turns out one of my PR fixes this case alrady: #57909
But what happens in the implementation here got me thinking... why do we even have multiple contravariant candidates here? we do have that because the inference infer from each of your signatures separately here. This is kinda a single call though. TS is able to combine union (or intersection) signatures into one. Shouldn't we then infer from that combined signature here?
So that led me to creating this experiment that also addresses this issue: #58482