Skip to content

Port #17771 to release-2.5 #18018

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 2 commits into from
Aug 24, 2017
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
6 changes: 5 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6584,6 +6584,10 @@ namespace ts {
return signature.resolvedReturnType;
}

function isResolvingReturnTypeOfSignature(signature: Signature) {
return !signature.resolvedReturnType && findResolutionCycleStartIndex(signature, TypeSystemPropertyName.ResolvedReturnType) >= 0;
}

function getRestTypeOfSignature(signature: Signature): Type {
if (signature.hasRestParameter) {
const type = getTypeOfSymbol(lastOrUndefined(signature.parameters));
Expand Down Expand Up @@ -12957,7 +12961,7 @@ namespace ts {
// Otherwise, if the containing function is contextually typed by a function type with exactly one call signature
// and that call signature is non-generic, return statements are contextually typed by the return type of the signature
const signature = getContextualSignatureForFunctionLikeDeclaration(<FunctionExpression>functionDecl);
if (signature) {
if (signature && !isResolvingReturnTypeOfSignature(signature)) {
return getReturnTypeOfSignature(signature);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/circularContextualReturnType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [circularContextualReturnType.ts]
// Repro from #17711

Object.freeze({
foo() {
return Object.freeze('a');
},
});


//// [circularContextualReturnType.js]
"use strict";
// Repro from #17711
Object.freeze({
foo: function () {
return Object.freeze('a');
}
});
19 changes: 19 additions & 0 deletions tests/baselines/reference/circularContextualReturnType.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== tests/cases/compiler/circularContextualReturnType.ts ===
// Repro from #17711

Object.freeze({
>Object.freeze : Symbol(ObjectConstructor.freeze, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>freeze : Symbol(ObjectConstructor.freeze, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

foo() {
>foo : Symbol(foo, Decl(circularContextualReturnType.ts, 2, 15))

return Object.freeze('a');
>Object.freeze : Symbol(ObjectConstructor.freeze, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>freeze : Symbol(ObjectConstructor.freeze, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

},
});

23 changes: 23 additions & 0 deletions tests/baselines/reference/circularContextualReturnType.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/circularContextualReturnType.ts ===
// Repro from #17711

Object.freeze({
>Object.freeze({ foo() { return Object.freeze('a'); },}) : Readonly<{ foo(): string; }>
>Object.freeze : { <T>(a: T[]): ReadonlyArray<T>; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>Object : ObjectConstructor
>freeze : { <T>(a: T[]): ReadonlyArray<T>; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>{ foo() { return Object.freeze('a'); },} : { foo(): string; }

foo() {
>foo : () => string

return Object.freeze('a');
>Object.freeze('a') : string
>Object.freeze : { <T>(a: T[]): ReadonlyArray<T>; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>Object : ObjectConstructor
>freeze : { <T>(a: T[]): ReadonlyArray<T>; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }
>'a' : "a"

},
});

9 changes: 9 additions & 0 deletions tests/cases/compiler/circularContextualReturnType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @strict: true

// Repro from #17711

Object.freeze({
foo() {
return Object.freeze('a');
},
});