Skip to content

Fix logic that infers from return types only #26678

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

Merged
merged 3 commits into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13549,14 +13549,16 @@ namespace ts {
const sourceLen = sourceSignatures.length;
const targetLen = targetSignatures.length;
const len = sourceLen < targetLen ? sourceLen : targetLen;
const skipParameters = !!(source.flags & TypeFlags.ContainsAnyFunctionType);
for (let i = 0; i < len; i++) {
inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getBaseSignature(targetSignatures[targetLen - len + i]));
inferFromSignature(getBaseSignature(sourceSignatures[sourceLen - len + i]), getBaseSignature(targetSignatures[targetLen - len + i]), skipParameters);
}
}

function inferFromSignature(source: Signature, target: Signature) {
forEachMatchingParameterType(source, target, inferFromContravariantTypes);

function inferFromSignature(source: Signature, target: Signature, skipParameters: boolean) {
if (!skipParameters) {
forEachMatchingParameterType(source, target, inferFromContravariantTypes);
}
const sourceTypePredicate = getTypePredicateOfSignature(source);
const targetTypePredicate = getTypePredicateOfSignature(target);
if (sourceTypePredicate && targetTypePredicate && sourceTypePredicate.kind === targetTypePredicate.kind) {
Expand Down Expand Up @@ -20576,8 +20578,10 @@ namespace ts {
return links.contextFreeType;
}
const returnType = getReturnTypeFromBody(node, checkMode);
const singleReturnSignature = createSignature(undefined, undefined, undefined, emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
return links.contextFreeType = createAnonymousType(node.symbol, emptySymbols, [singleReturnSignature], emptyArray, undefined, undefined);
const returnOnlySignature = createSignature(undefined, undefined, undefined, emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
const returnOnlyType = createAnonymousType(node.symbol, emptySymbols, [returnOnlySignature], emptyArray, undefined, undefined);
returnOnlyType.flags |= TypeFlags.ContainsAnyFunctionType;
return links.contextFreeType = returnOnlyType;
}
return anyFunctionType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//// [badInferenceLowerPriorityThanGoodInference.ts]
// Repro from #13118

interface Foo<A> {
a: A;
b: (x: A) => void;
Expand All @@ -11,11 +13,24 @@ const result = canYouInferThis(() => ({
b: x => { }
}))

result.BLAH;
result.BLAH;

// Repro from #26629

function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}

goofus((a: string) => ({ dog() { return a; } }));
goofus((a: string) => ({ dog: function() { return a; } }));


//// [badInferenceLowerPriorityThanGoodInference.js]
// Repro from #13118
var result = canYouInferThis(function () { return ({
a: { BLAH: 33 },
b: function (x) { }
}); });
result.BLAH;
// Repro from #26629
function goofus(f) { }
goofus(function (a) { return ({ dog: function () { return a; } }); });
goofus(function (a) { return ({ dog: function () { return a; } }); });
Original file line number Diff line number Diff line change
@@ -1,42 +1,65 @@
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
// Repro from #13118

interface Foo<A> {
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))

a: A;
>a : Symbol(Foo.a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 18))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
>a : Symbol(Foo.a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 18))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))

b: (x: A) => void;
>b : Symbol(Foo.b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 1, 9))
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 8))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))
>b : Symbol(Foo.b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 9))
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 4, 8))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 2, 14))
}

declare function canYouInferThis<A>(fn: () => Foo<A>): A;
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 1))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
>fn : Symbol(fn, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 36))
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 1))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))
>fn : Symbol(fn, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 36))
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 33))

const result = canYouInferThis(() => ({
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 5))
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 3, 1))
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 5))
>canYouInferThis : Symbol(canYouInferThis, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 1))

a: { BLAH: 33 },
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 39))
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 39))
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))

b: x => { }
>b : Symbol(b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 20))
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 6))
>b : Symbol(b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 20))
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 11, 6))

}))

result.BLAH;
>result.BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 7, 5))
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 8))
>result.BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))
>result : Symbol(result, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 5))
>BLAH : Symbol(BLAH, Decl(badInferenceLowerPriorityThanGoodInference.ts, 10, 8))

// Repro from #26629

function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
>ARGS : Symbol(ARGS, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 17))
>f : Symbol(f, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 38))
>args : Symbol(args, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 42))
>ARGS : Symbol(ARGS, Decl(badInferenceLowerPriorityThanGoodInference.ts, 18, 17))

goofus((a: string) => ({ dog() { return a; } }));
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 8))
>dog : Symbol(dog, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 24))
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 20, 8))

goofus((a: string) => ({ dog: function() { return a; } }));
>goofus : Symbol(goofus, Decl(badInferenceLowerPriorityThanGoodInference.ts, 14, 12))
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 8))
>dog : Symbol(dog, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 24))
>a : Symbol(a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 21, 8))

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
// Repro from #13118

interface Foo<A> {
a: A;
>a : A
Expand Down Expand Up @@ -38,3 +40,31 @@ result.BLAH;
>result : { BLAH: number; }
>BLAH : number

// Repro from #26629

function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
>f : (...args: ARGS) => any
>args : ARGS

goofus((a: string) => ({ dog() { return a; } }));
>goofus((a: string) => ({ dog() { return a; } })) : void
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
>(a: string) => ({ dog() { return a; } }) : (a: string) => { dog(): string; }
>a : string
>({ dog() { return a; } }) : { dog(): string; }
>{ dog() { return a; } } : { dog(): string; }
>dog : () => string
>a : string

goofus((a: string) => ({ dog: function() { return a; } }));
>goofus((a: string) => ({ dog: function() { return a; } })) : void
>goofus : <ARGS extends any[]>(f: (...args: ARGS) => any) => void
>(a: string) => ({ dog: function() { return a; } }) : (a: string) => { dog: () => string; }
>a : string
>({ dog: function() { return a; } }) : { dog: () => string; }
>{ dog: function() { return a; } } : { dog: () => string; }
>dog : () => string
>function() { return a; } : () => string
>a : string

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Repro from #13118

interface Foo<A> {
a: A;
b: (x: A) => void;
Expand All @@ -10,4 +12,11 @@ const result = canYouInferThis(() => ({
b: x => { }
}))

result.BLAH;
result.BLAH;

// Repro from #26629

function goofus <ARGS extends any[]> (f: (...args: ARGS) => any ) {}

goofus((a: string) => ({ dog() { return a; } }));
goofus((a: string) => ({ dog: function() { return a; } }));