fix(expect, @jest/expect): do not infer argument types of *CalledWith
and *ReturnedWith
matchers
#13339
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #13337
Summary
Unfortunately #13268 and #13278 have to be reverted. As reported, type inference does not work with function overloads. I have to add that it also does not work with asymmetric matchers.
The asymmetric matchers return
AsymmetricMatcher
type. To make the inference work with an argument of[a: string]
type, it is needed to remap it into[a: string | AsymmetricMatcher]
. Could be possible with something like:A tuple should be broken into elements and turned into remapped tuple. And this is the hard part:
The optional args are simply discarded during inference. That is because TypeScript assumes that the optional args are simply not there. It could be there or it could not in a tuple type, but if a type of tuple member is turned into a type of a variable? There isn’t such a thing as an optional variable. That’s why the
Remapped
type has no chance to work.I couldn’t find a solution. Perhaps it makes sense to revert it for now, because types are obviously buggy.
Test plan
Type tests with asymmetric matchers are added for the future. These are failing on
main
.