MockLink: update the function types returned by ResultFunction
& VariableMatcher
to define their own generics
#11812
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.
Issue
A feature introduced last year (#6701) which updated
MockLink
'sResultFunction
type and introduced a newVariableMatcher
type (both referenced byMockedResponse
) has caused a type issue our repository that is affecting our team's common practice of setting a generic whichextends
fromMockedResponse[]
on our test setup functions (e.g.function setup<M extends MockedResponse[]>
).This issue arises due to both types accepting a variables generic type (
V
), and then returning a function type which uses this generic as its own argument type:When these types are combined with our usage of
M extends MockedResponse[]
, TypeScript is unable to correctly infer thevariables
argument type for each of of the values inM
.Example Failure
Here's an isolated example of this which will currently fail type checking:
Failing Example:
Proposed Fix
I believe this can be fixed, without causing any breaking changes, by making the function types returned by
ResultFunction
&VariableMatcher
declare their own generic type, which extends from and defaults to theV
type.This resolves the issue in the snippet above:
Example Fix:
I've implemented the change as part of this PR and added a test case which currently fails when run against
main
, but passes on this branch.Please let me know if you have any questions / concerns about the changes — thanks!