Skip to content

Commit f3691a3

Browse files
committed
Allow parameters to be contextually-typed based on contextual rest instantiated using return mapper
1 parent e635bb9 commit f3691a3

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39547,7 +39547,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3954739547
inferFromAnnotatedParametersAndReturn(signature, contextualSignature, inferenceContext!);
3954839548
const restType = getEffectiveRestType(contextualSignature);
3954939549
if (restType && restType.flags & TypeFlags.TypeParameter) {
39550-
instantiatedContextualSignature = instantiateSignature(contextualSignature, inferenceContext!.nonFixingMapper);
39550+
const info = find(inferenceContext!.inferences, info => info.typeParameter === restType);
39551+
const mapper = combineTypeMappers(info && !hasInferenceCandidates(info) ? inferenceContext!.returnMapper : undefined, inferenceContext!.nonFixingMapper);
39552+
instantiatedContextualSignature = instantiateSignature(contextualSignature, mapper);
3955139553
}
3955239554
}
3955339555
instantiatedContextualSignature ||= inferenceContext ?
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//// [tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts] ////
2+
3+
=== contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62336
5+
6+
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown;
7+
>call : Symbol(call, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 0, 0))
8+
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22))
9+
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 25))
10+
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 30))
11+
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 40))
12+
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22))
13+
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 61))
14+
>T : Symbol(T, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 22))
15+
16+
declare function fn<Args extends any[]>(
17+
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 78))
18+
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20))
19+
20+
fn: (...args: Args) => unknown,
21+
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 40))
22+
>args : Symbol(args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 5, 7))
23+
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20))
24+
25+
): (...args: Args) => unknown;
26+
>args : Symbol(args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 6, 4))
27+
>Args : Symbol(Args, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 4, 20))
28+
29+
call(
30+
>call : Symbol(call, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 0, 0))
31+
32+
fn(function (a, b: number) {
33+
>fn : Symbol(fn, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 2, 78))
34+
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 15))
35+
>b : Symbol(b, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 17))
36+
37+
a; // string
38+
>a : Symbol(a, Decl(contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts, 9, 15))
39+
40+
}),
41+
);
42+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [tests/cases/compiler/contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts] ////
2+
3+
=== contextuallyTypeParametersUsingInstantiantedRestReturnMapper1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/62336
5+
6+
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown;
7+
>call : <T>(fn: (a: string, b: T) => unknown) => (b: T) => unknown
8+
> : ^ ^^ ^^ ^^^^^
9+
>fn : (a: string, b: T) => unknown
10+
> : ^ ^^ ^^ ^^ ^^^^^
11+
>a : string
12+
> : ^^^^^^
13+
>b : T
14+
> : ^
15+
>b : T
16+
> : ^
17+
18+
declare function fn<Args extends any[]>(
19+
>fn : <Args extends any[]>(fn: (...args: Args) => unknown) => (...args: Args) => unknown
20+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
21+
22+
fn: (...args: Args) => unknown,
23+
>fn : (...args: Args) => unknown
24+
> : ^^^^ ^^ ^^^^^
25+
>args : Args
26+
> : ^^^^
27+
28+
): (...args: Args) => unknown;
29+
>args : Args
30+
> : ^^^^
31+
32+
call(
33+
>call( fn(function (a, b: number) { a; // string }),) : (b: number) => unknown
34+
> : ^ ^^^^^^^^^^^^^
35+
>call : <T>(fn: (a: string, b: T) => unknown) => (b: T) => unknown
36+
> : ^ ^^ ^^ ^^^^^
37+
38+
fn(function (a, b: number) {
39+
>fn(function (a, b: number) { a; // string }) : (a: string, b: number) => unknown
40+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
>fn : <Args extends any[]>(fn: (...args: Args) => unknown) => (...args: Args) => unknown
42+
> : ^ ^^^^^^^^^ ^^ ^^ ^^^^^
43+
>function (a, b: number) { a; // string } : (a: string, b: number) => void
44+
> : ^ ^^^^^^^^^^ ^^ ^^^^^^^^^
45+
>a : string
46+
> : ^^^^^^
47+
>b : number
48+
> : ^^^^^^
49+
50+
a; // string
51+
>a : string
52+
> : ^^^^^^
53+
54+
}),
55+
);
56+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/62336
5+
6+
declare function call<T>(fn: (a: string, b: T) => unknown): (b: T) => unknown;
7+
8+
declare function fn<Args extends any[]>(
9+
fn: (...args: Args) => unknown,
10+
): (...args: Args) => unknown;
11+
12+
call(
13+
fn(function (a, b: number) {
14+
a; // string
15+
}),
16+
);

0 commit comments

Comments
 (0)