Merge pull request #17771 from Microsoft/fixCircularContextualType
Fix circular contextual return type error
This commit is contained in:
commit
ce2ac1751e
5 changed files with 74 additions and 1 deletions
|
@ -6582,6 +6582,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));
|
||||
|
@ -12955,7 +12959,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);
|
||||
}
|
||||
|
||||
|
|
18
tests/baselines/reference/circularContextualReturnType.js
Normal file
18
tests/baselines/reference/circularContextualReturnType.js
Normal file
|
@ -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');
|
||||
}
|
||||
});
|
|
@ -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
tests/baselines/reference/circularContextualReturnType.types
Normal file
23
tests/baselines/reference/circularContextualReturnType.types
Normal file
|
@ -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
tests/cases/compiler/circularContextualReturnType.ts
Normal file
9
tests/cases/compiler/circularContextualReturnType.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
// @strict: true
|
||||
|
||||
// Repro from #17711
|
||||
|
||||
Object.freeze({
|
||||
foo() {
|
||||
return Object.freeze('a');
|
||||
},
|
||||
});
|
Loading…
Reference in a new issue