-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Open
Andarist
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
Andarist:contextual-parameter-type-using-rest-return-mapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−1
Open
Allow parameters to be contextually-typed based on contextual rest instantiated using return mapper #62341
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...baselines/reference/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//// [tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts] //// | ||
|
||
=== contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/62336 | ||
|
||
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown; | ||
>call : Symbol(call, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 0, 0)) | ||
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22)) | ||
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 25)) | ||
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 30)) | ||
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 40)) | ||
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22)) | ||
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 61)) | ||
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22)) | ||
|
||
declare function fn<Args extends any[]>( | ||
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 78)) | ||
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20)) | ||
|
||
fn: (...args: Args) => unknown, | ||
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 40)) | ||
>args : Symbol(args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 5, 7)) | ||
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20)) | ||
|
||
): (...args: Args) => unknown; | ||
>args : Symbol(args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 6, 4)) | ||
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20)) | ||
|
||
call( | ||
>call : Symbol(call, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 0, 0)) | ||
|
||
fn(function (a, b: number) { | ||
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 78)) | ||
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 15)) | ||
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 17)) | ||
|
||
a; // string | ||
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 15)) | ||
|
||
}), | ||
); | ||
|
56 changes: 56 additions & 0 deletions
56
...s/baselines/reference/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//// [tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts] //// | ||
|
||
=== contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts === | ||
// https://github.com/microsoft/TypeScript/issues/62336 | ||
|
||
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown; | ||
>call : <T>(fn: (a: string, b: T) => unknown) => (b: T) => unknown | ||
> : ^ ^^ ^^ ^^^^^ | ||
>fn : (a: string, b: T) => unknown | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>a : string | ||
> : ^^^^^^ | ||
>b : T | ||
> : ^ | ||
>b : T | ||
> : ^ | ||
|
||
declare function fn<Args extends any[]>( | ||
>fn : <Args extends any[]>(fn: (...args: Args) => unknown) => (...args: Args) => unknown | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
|
||
fn: (...args: Args) => unknown, | ||
>fn : (...args: Args) => unknown | ||
> : ^^^^ ^^ ^^^^^ | ||
>args : Args | ||
> : ^^^^ | ||
|
||
): (...args: Args) => unknown; | ||
>args : Args | ||
> : ^^^^ | ||
|
||
call( | ||
>call( fn(function (a, b: number) { a; // string }),) : (b: number) => unknown | ||
> : ^ ^^^^^^^^^^^^^ | ||
>call : <T>(fn: (a: string, b: T) => unknown) => (b: T) => unknown | ||
> : ^ ^^ ^^ ^^^^^ | ||
|
||
fn(function (a, b: number) { | ||
>fn(function (a, b: number) { a; // string }) : (a: string, b: number) => unknown | ||
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
>fn : <Args extends any[]>(fn: (...args: Args) => unknown) => (...args: Args) => unknown | ||
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^ | ||
>function (a, b: number) { a; // string } : (a: string, b: number) => void | ||
> : ^ ^^^^^^^^^^ ^^ ^^^^^^^^^ | ||
>a : string | ||
> : ^^^^^^ | ||
>b : number | ||
> : ^^^^^^ | ||
|
||
a; // string | ||
>a : string | ||
> : ^^^^^^ | ||
|
||
}), | ||
); | ||
|
16 changes: 16 additions & 0 deletions
16
tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/62336 | ||
|
||
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown; | ||
|
||
declare function fn<Args extends any[]>( | ||
fn: (...args: Args) => unknown, | ||
): (...args: Args) => unknown; | ||
|
||
call( | ||
fn(function (a, b: number) { | ||
a; // string | ||
}), | ||
); | ||
Andarist marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
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.
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 sameArgs
.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.