Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10241,7 +10241,10 @@ namespace ts {
return true;
}
}
return hasContextSensitiveReturnExpression(node);
}

function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
// TODO(anhans): A block should be context-sensitive if it has a context-sensitive return value.
const body = node.body!;
return body.kind === SyntaxKind.Block ? false : isContextSensitive(body);
Expand Down Expand Up @@ -20543,6 +20546,12 @@ namespace ts {

// The identityMapper object is used to indicate that function expressions are wildcards
if (checkMode === CheckMode.SkipContextSensitive && isContextSensitive(node)) {
// Skip parameters, return signature with return type that retains noncontextual parts so inferences can still be drawn in an early stage
if (!getEffectiveReturnTypeNode(node) && hasContextSensitiveReturnExpression(node)) {
const returnType = getReturnTypeFromBody(node, checkMode);
const singleReturnSignature = createSignature(undefined, undefined, undefined, emptyArray, returnType, /*resolvedTypePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false);
return createAnonymousType(node.symbol, emptySymbols, [singleReturnSignature], emptyArray, undefined, undefined);

@weswigham weswigham Jul 25, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth caching this type in some way? Like having an internal () => T generic that I simply instantiate here?

}
return anyFunctionType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [badInferenceLowerPriorityThanGoodInference.ts]
interface Foo<A> {
a: A;
b: (x: A) => void;
}

declare function canYouInferThis<A>(fn: () => Foo<A>): A;

const result = canYouInferThis(() => ({
a: { BLAH: 33 },
b: x => { }
}))

result.BLAH;

//// [badInferenceLowerPriorityThanGoodInference.js]
var result = canYouInferThis(function () { return ({
a: { BLAH: 33 },
b: function (x) { }
}); });
result.BLAH;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
interface Foo<A> {
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 14))

a: A;
>a : Symbol(Foo.a, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 18))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 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))
}

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))
>Foo : Symbol(Foo, Decl(badInferenceLowerPriorityThanGoodInference.ts, 0, 0))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))
>A : Symbol(A, Decl(badInferenceLowerPriorityThanGoodInference.ts, 5, 33))

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

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

b: x => { }
>b : Symbol(b, Decl(badInferenceLowerPriorityThanGoodInference.ts, 8, 20))
>x : Symbol(x, Decl(badInferenceLowerPriorityThanGoodInference.ts, 9, 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))

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts ===
interface Foo<A> {
>Foo : Foo<A>
>A : A

a: A;
>a : A
>A : A

b: (x: A) => void;
>b : (x: A) => void
>x : A
>A : A
}

declare function canYouInferThis<A>(fn: () => Foo<A>): A;
>canYouInferThis : <A>(fn: () => Foo<A>) => A
>A : A
>fn : () => Foo<A>
>Foo : Foo<A>
>A : A
>A : A

const result = canYouInferThis(() => ({
>result : { BLAH: number; }
>canYouInferThis(() => ({ a: { BLAH: 33 }, b: x => { }})) : { BLAH: number; }
>canYouInferThis : <A>(fn: () => Foo<A>) => A
>() => ({ a: { BLAH: 33 }, b: x => { }}) : () => { a: { BLAH: number; }; b: (x: { BLAH: number; }) => void; }
>({ a: { BLAH: 33 }, b: x => { }}) : { a: { BLAH: number; }; b: (x: { BLAH: number; }) => void; }
>{ a: { BLAH: 33 }, b: x => { }} : { a: { BLAH: number; }; b: (x: { BLAH: number; }) => void; }

a: { BLAH: 33 },
>a : { BLAH: number; }
>{ BLAH: 33 } : { BLAH: number; }
>BLAH : number
>33 : 33

b: x => { }
>b : (x: { BLAH: number; }) => void
>x => { } : (x: { BLAH: number; }) => void
>x : { BLAH: number; }

}))

result.BLAH;
>result.BLAH : number
>result : { BLAH: number; }
>BLAH : number

13 changes: 13 additions & 0 deletions tests/cases/compiler/badInferenceLowerPriorityThanGoodInference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Foo<A> {
a: A;
b: (x: A) => void;
}

declare function canYouInferThis<A>(fn: () => Foo<A>): A;

const result = canYouInferThis(() => ({
a: { BLAH: 33 },
b: x => { }
}))

result.BLAH;