-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Allow parameters to be contextually-typed based on contextual rest instantiated using return mapper #62341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Allow parameters to be contextually-typed based on contextual rest instantiated using return mapper #62341
Conversation
…stantiated using return mapper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes an issue where parameters were not being contextually typed when using an instantiated rest return mapper. The change allows the TypeScript compiler to properly infer parameter types in complex generic scenarios involving rest parameters.
Key Changes
- Updates type inference logic in the checker to use return mapper when appropriate for rest type parameters
- Adds a test case demonstrating the fixed contextual typing behavior
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/compiler/checker.ts | Modifies type inference logic to combine return mapper with non-fixing mapper for rest type parameters |
| tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts | Adds test case for the contextual typing fix |
| tests/baselines/reference/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.types | Baseline file showing expected type inference results |
| tests/baselines/reference/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.symbols | Baseline file showing expected symbol resolution |
tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts
Show resolved
Hide resolved
| ): (...args: Args) => unknown; | ||
|
|
||
| call( | ||
| fn(function (a, b: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the presented case, there are no regular inference candidates for this rest because the return type candidate that could be gathered is only partially inferable ([a: string, b: silentNeverType]). That means it's treated as non-inferrable candidate and thus it's ignored.
But the return mapper manages to capture a [a: string, b: unknown] inference for the same Args.
So the proposal here is to utilize that mapping in the absence of regular candidates as that allows the inferrable part of the original partially candidate to be used when contextually typing parameters. Of course, that inferrable part is coming now from a completely different place (return mapper instead of that original partially inferrable candidate) - but conceptually they both used the information from the contextual signature so their "root origin" is the same.
…type-using-rest-return-mapper
fixes #62336