Skip to content

Commit 73ec596

Browse files
authored
Merge pull request #26678 from Microsoft/fixReturnOnlyInference
Fix logic that infers from return types only
2 parents b5998d9 + ed97443 commit 73ec596

5 files changed

+109
-28
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13549,14 +13549,16 @@ namespace ts {
1354913549
const sourceLen = sourceSignatures.length;
1355013550
const targetLen = targetSignatures.length;
1355113551
const len = sourceLen < targetLen ? sourceLen : targetLen;
13552+
const skipParameters = !!(source.flags & TypeFlags.ContainsAnyFunctionType);
1355213553
for (let i = 0; i < len; i++) {
13553-
inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getBaseSignature(targetSignatures[targetLen - len + i]));
13554+
inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getBaseSignature(targetSignatures[targetLen - len + i]), skipParameters);
1355413555
}
1355513556
}
1355613557

13557-
function inferFromSignature(source: Signature, target: Signature) {
13558-
forEachMatchingParameterType(source, target, inferFromContravariantTypes);
13559-
13558+
function inferFromSignature(source: Signature, target: Signature, skipParameters: boolean) {
13559+
if (!skipParameters) {
13560+
forEachMatchingParameterType(source, target, inferFromContravariantTypes);
13561+
}
1356013562
const sourceTypePredicate = getTypePredicateOfSignature(source);
1356113563
const targetTypePredicate = getTypePredicateOfSignature(target);
1356213564
if (sourceTypePredicate && targetTypePredicate && sourceTypePredicate.kind === targetTypePredicate.kind) {
@@ -20576,8 +20578,10 @@ namespace ts {
2057620578
return links.contextFreeType;
2057720579
}
2057820580
const returnType = getReturnTypeFromBody(node, checkMode);
20579-
const singleReturnSignature = createSignature(undefined, undefined, undefined, emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
20580-
return links.contextFreeType = createAnonymousType(node.symbol, emptySymbols, [singleReturnSignature], emptyArray, undefined, undefined);
20581+
const returnOnlySignature = createSignature(undefined, undefined, undefined, emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
20582+
const returnOnlyType = createAnonymousType(node.symbol, emptySymbols, [returnOnlySignature], emptyArray, undefined, undefined);
20583+
returnOnlyType.flags |= TypeFlags.ContainsAnyFunctionType;
20584+
return links.contextFreeType = returnOnlyType;
2058120585
}
2058220586
return anyFunctionType;
2058320587
}

tests/baselines/reference/badInferenceLowerPriorityThanGoodInference.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//// [badInferenceLowerPriorityThanGoodInference.ts]
2+
// Repro from #13118
3+
24
interface Foo<A> {
35
a: A;
46
b: (x: A) => void;
@@ -11,11 +13,24 @@ const result = canYouInferThis(() => ({
1113
b: x => { }
1214
}))
1315

14-
result.BLAH;
16+
result.BLAH;
17+
18+
// Repro from #26629
19+
20+
function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
21+
22+
goofus((a: string) => ({ dog() { return a; } }));
23+
goofus((a: string) => ({ dog: function() { return a; } }));
24+
1525

1626
//// [badInferenceLowerPriorityThanGoodInference.js]
27+
// Repro from #13118
1728
var result = canYouInferThis(function () { return ({
1829
a: { BLAH: 33 },
1930
b: function (x) { }
2031
}); });
2132
result.BLAH;
33+
// Repro from #26629
34+
function goofus(f) { }
35+
goofus(function (a) { return ({ dog: function () { return a; } }); });
36+
goofus(function (a) { return ({ dog: function () { return a; } }); });
Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
11
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
2+
// Repro from #13118
3+
24
interface Foo<A> {
35
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
4-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
6+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))
57

68
a: A;
7-
>a : Symbol(Foo.a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 18))
8-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
9+
>a : Symbol(Foo.a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 18))
10+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))
911

1012
b: (x: A) => void;
11-
>b : Symbol(Foo.b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 1, 9))
12-
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 8))
13-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
13+
>b : Symbol(Foo.b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 9))
14+
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 4, 8))
15+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))
1416
}
1517

1618
declare function canYouInferThis<A>(fn: () => Foo<A>): A;
17-
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 1))
18-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
19-
>fn : Symbol(fn, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 36))
19+
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 1))
20+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))
21+
>fn : Symbol(fn, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 36))
2022
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
21-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
22-
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
23+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))
24+
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))
2325

2426
const result = canYouInferThis(() => ({
25-
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 5))
26-
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 1))
27+
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 5))
28+
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 1))
2729

2830
a: { BLAH: 33 },
29-
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 39))
30-
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
31+
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 39))
32+
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))
3133

3234
b: x => { }
33-
>b : Symbol(b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 20))
34-
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 6))
35+
>b : Symbol(b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 20))
36+
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 11, 6))
3537

3638
}))
3739

3840
result.BLAH;
39-
>result.BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
40-
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 5))
41-
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
41+
>result.BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))
42+
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 5))
43+
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))
44+
45+
// Repro from #26629
46+
47+
function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
48+
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
49+
>ARGS : Symbol(ARGS, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 17))
50+
>f : Symbol(f, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 38))
51+
>args : Symbol(args, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 42))
52+
>ARGS : Symbol(ARGS, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 17))
53+
54+
goofus((a: string) => ({ dog() { return a; } }));
55+
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
56+
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 8))
57+
>dog : Symbol(dog, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 24))
58+
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 8))
59+
60+
goofus((a: string) => ({ dog: function() { return a; } }));
61+
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
62+
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 8))
63+
>dog : Symbol(dog, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 24))
64+
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 8))
4265

tests/baselines/reference/badInferenceLowerPriorityThanGoodInference.types

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
2+
// Repro from #13118
3+
24
interface Foo<A> {
35
a: A;
46
>a : A
@@ -38,3 +40,31 @@ result.BLAH;
3840
>result : { BLAH: number; }
3941
>BLAH : number
4042

43+
// Repro from #26629
44+
45+
function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
46+
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
47+
>f : (...args: ARGS) => any
48+
>args : ARGS
49+
50+
goofus((a: string) => ({ dog() { return a; } }));
51+
>goofus((a: string) => ({ dog() { return a; } })) : void
52+
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
53+
>(a: string) => ({ dog() { return a; } }) : (a: string) => { dog(): string; }
54+
>a : string
55+
>({ dog() { return a; } }) : { dog(): string; }
56+
>{ dog() { return a; } } : { dog(): string; }
57+
>dog : () => string
58+
>a : string
59+
60+
goofus((a: string) => ({ dog: function() { return a; } }));
61+
>goofus((a: string) => ({ dog: function() { return a; } })) : void
62+
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
63+
>(a: string) => ({ dog: function() { return a; } }) : (a: string) => { dog: () => string; }
64+
>a : string
65+
>({ dog: function() { return a; } }) : { dog: () => string; }
66+
>{ dog: function() { return a; } } : { dog: () => string; }
67+
>dog : () => string
68+
>function() { return a; } : () => string
69+
>a : string
70+

tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Repro from #13118
2+
13
interface Foo<A> {
24
a: A;
35
b: (x: A) => void;
@@ -10,4 +12,11 @@ const result = canYouInferThis(() => ({
1012
b: x => { }
1113
}))
1214

13-
result.BLAH;
15+
result.BLAH;
16+
17+
// Repro from #26629
18+
19+
function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
20+
21+
goofus((a: string) => ({ dog() { return a; } }));
22+
goofus((a: string) => ({ dog: function() { return a; } }));

0 commit comments

Comments
 (0)