Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39547,7 +39547,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
inferFromAnnotatedParametersAndReturn(signature, contextualSignature, inferenceContext!);
const restType = getEffectiveRestType(contextualSignature);
if (restType && restType.flags & TypeFlags.TypeParameter) {
instantiatedContextualSignature = instantiateSignature(contextualSignature, inferenceContext!.nonFixingMapper);
const info = find(inferenceContext!.inferences, info => info.typeParameter === restType);
const mapper = combineTypeMappers(info && !hasInferenceCandidates(info) ? inferenceContext!.returnMapper : undefined, inferenceContext!.nonFixingMapper);
instantiatedContextualSignature = instantiateSignature(contextualSignature, mapper);
}
}
instantiatedContextualSignature ||= inferenceContext ?
Expand Down
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))

}),
);

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
> : ^^^^^^

}),
);

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) {
Copy link
Contributor Author

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.

a; // string
}),
);
Loading