diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 86acdb7c44b6d..d9af70cd037d9 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -25172,13 +25172,17 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function applyToReturnTypes(source: Signature, target: Signature, callback: (s: Type, t: Type) => void) { - const sourceTypePredicate = getTypePredicateOfSignature(source); const targetTypePredicate = getTypePredicateOfSignature(target); - if (sourceTypePredicate && targetTypePredicate && typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.type && targetTypePredicate.type) { - callback(sourceTypePredicate.type, targetTypePredicate.type); + if (targetTypePredicate) { + const sourceTypePredicate = getTypePredicateOfSignature(source); + if (sourceTypePredicate && typePredicateKindsMatch(sourceTypePredicate, targetTypePredicate) && sourceTypePredicate.type && targetTypePredicate.type) { + callback(sourceTypePredicate.type, targetTypePredicate.type); + return; + } } - else { - callback(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + const targetReturnType = getReturnTypeOfSignature(target); + if (couldContainTypeVariables(targetReturnType)) { + callback(getReturnTypeOfSignature(source), targetReturnType); } } diff --git a/tests/baselines/reference/circularReferenceInReturnType.errors.txt b/tests/baselines/reference/circularReferenceInReturnType.errors.txt index bae89c5c1520c..b94a93182ce0f 100644 --- a/tests/baselines/reference/circularReferenceInReturnType.errors.txt +++ b/tests/baselines/reference/circularReferenceInReturnType.errors.txt @@ -1,10 +1,8 @@ circularReferenceInReturnType.ts(3,7): error TS7022: 'res1' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. circularReferenceInReturnType.ts(3,18): error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. -circularReferenceInReturnType.ts(9,7): error TS7022: 'res3' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. -circularReferenceInReturnType.ts(9,20): error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. -==== circularReferenceInReturnType.ts (4 errors) ==== +==== circularReferenceInReturnType.ts (2 errors) ==== // inference fails for res1 and res2, but ideally should not declare function fn1(cb: () => T): string; const res1 = fn1(() => res1); @@ -18,8 +16,14 @@ circularReferenceInReturnType.ts(9,20): error TS7024: Function implicitly has re declare function fn3(): (cb: (arg: T2) => any) => (a: T) => void; const res3 = fn3()(() => res3); - ~~~~ -!!! error TS7022: 'res3' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer. - ~~~~~~~~~~ -!!! error TS7024: Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions. + + // https://github.com/microsoft/TypeScript/issues/58616 + + function foo(arg: Parameters[0]) { + return arg; + } + + function bar(arg: string) { + return foo(arg); + } \ No newline at end of file diff --git a/tests/baselines/reference/circularReferenceInReturnType.symbols b/tests/baselines/reference/circularReferenceInReturnType.symbols index d126adc332884..72d98a41f140a 100644 --- a/tests/baselines/reference/circularReferenceInReturnType.symbols +++ b/tests/baselines/reference/circularReferenceInReturnType.symbols @@ -40,3 +40,24 @@ const res3 = fn3()(() => res3); >fn3 : Symbol(fn3, Decl(circularReferenceInReturnType.ts, 5, 31)) >res3 : Symbol(res3, Decl(circularReferenceInReturnType.ts, 8, 5)) +// https://github.com/microsoft/TypeScript/issues/58616 + +function foo(arg: Parameters[0]) { +>foo : Symbol(foo, Decl(circularReferenceInReturnType.ts, 8, 31)) +>arg : Symbol(arg, Decl(circularReferenceInReturnType.ts, 12, 13)) +>Parameters : Symbol(Parameters, Decl(lib.es5.d.ts, --, --)) +>bar : Symbol(bar, Decl(circularReferenceInReturnType.ts, 14, 1)) + + return arg; +>arg : Symbol(arg, Decl(circularReferenceInReturnType.ts, 12, 13)) +} + +function bar(arg: string) { +>bar : Symbol(bar, Decl(circularReferenceInReturnType.ts, 14, 1)) +>arg : Symbol(arg, Decl(circularReferenceInReturnType.ts, 16, 13)) + + return foo(arg); +>foo : Symbol(foo, Decl(circularReferenceInReturnType.ts, 8, 31)) +>arg : Symbol(arg, Decl(circularReferenceInReturnType.ts, 16, 13)) +} + diff --git a/tests/baselines/reference/circularReferenceInReturnType.types b/tests/baselines/reference/circularReferenceInReturnType.types index 37d311ac83852..bcc174e7be663 100644 --- a/tests/baselines/reference/circularReferenceInReturnType.types +++ b/tests/baselines/reference/circularReferenceInReturnType.types @@ -53,16 +53,46 @@ declare function fn3(): (cb: (arg: T2) => any) => (a: T) => void; > : ^ const res3 = fn3()(() => res3); ->res3 : any -> : ^^^ +>res3 : (a: unknown) => void +> : ^ ^^^^^^^^^^^^^^ >fn3()(() => res3) : (a: unknown) => void > : ^ ^^^^^^^^^^^^^^ >fn3() : (cb: (arg: T2) => any) => (a: unknown) => void > : ^^^^^ ^^^ ^^^^^^^^^ ^^^^^ ^^^^^^^ >fn3 : () => (cb: (arg: T2) => any) => (a: T) => void > : ^ ^^^^^^^ ->() => res3 : () => any -> : ^^^^^^^^^ ->res3 : any -> : ^^^ +>() => res3 : () => (a: unknown) => void +> : ^^^^^^^ ^^^^^^^^^^^^^^ +>res3 : (a: unknown) => void +> : ^ ^^^^^^^^^^^^^^ + +// https://github.com/microsoft/TypeScript/issues/58616 + +function foo(arg: Parameters[0]) { +>foo : (arg: Parameters[0]) => string +> : ^ ^^ ^^^^^^^^^^^ +>arg : string +> : ^^^^^^ +>bar : (arg: string) => string +> : ^ ^^ ^^^^^^^^^^^ + + return arg; +>arg : string +> : ^^^^^^ +} + +function bar(arg: string) { +>bar : (arg: string) => string +> : ^ ^^ ^^^^^^^^^^^ +>arg : string +> : ^^^^^^ + + return foo(arg); +>foo(arg) : string +> : ^^^^^^ +>foo : (arg: Parameters[0]) => string +> : ^ ^^ ^^^^^^^^^^^ +>arg : string +> : ^^^^^^ +} diff --git a/tests/baselines/reference/complexRecursiveCollections.types b/tests/baselines/reference/complexRecursiveCollections.types index 11d60bdbb0778..eba0f913b22ab 100644 --- a/tests/baselines/reference/complexRecursiveCollections.types +++ b/tests/baselines/reference/complexRecursiveCollections.types @@ -3,7 +3,7 @@ === Performance Stats === Assignability cache: 10,000 Type Count: 50,000 -Instantiation count: 250,000 +Instantiation count: 100,000 Symbol count: 100,000 === complex.ts === diff --git a/tests/baselines/reference/flatArrayNoExcessiveStackDepth.errors.txt b/tests/baselines/reference/flatArrayNoExcessiveStackDepth.errors.txt index fee12ed489776..d4ae5a13e8947 100644 --- a/tests/baselines/reference/flatArrayNoExcessiveStackDepth.errors.txt +++ b/tests/baselines/reference/flatArrayNoExcessiveStackDepth.errors.txt @@ -1,6 +1,6 @@ flatArrayNoExcessiveStackDepth.ts(20,5): error TS2322: Type 'FlatArray' is not assignable to type 'FlatArray'. Type 'Arr' is not assignable to type 'FlatArray'. - Type 'Arr' is not assignable to type '(Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr) & Arr'. + Type 'Arr' is not assignable to type 'Arr & (Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr)'. Type 'Arr' is not assignable to type 'Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr'. @@ -28,7 +28,7 @@ flatArrayNoExcessiveStackDepth.ts(20,5): error TS2322: Type 'FlatArray ~ !!! error TS2322: Type 'FlatArray' is not assignable to type 'FlatArray'. !!! error TS2322: Type 'Arr' is not assignable to type 'FlatArray'. -!!! error TS2322: Type 'Arr' is not assignable to type '(Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr) & Arr'. +!!! error TS2322: Type 'Arr' is not assignable to type 'Arr & (Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr)'. !!! error TS2322: Type 'Arr' is not assignable to type 'Arr extends readonly (infer InnerArr)[] ? FlatArray : Arr'. } \ No newline at end of file diff --git a/tests/cases/compiler/circularReferenceInReturnType.ts b/tests/cases/compiler/circularReferenceInReturnType.ts index 50f82f9e37156..ec53b6d50731f 100644 --- a/tests/cases/compiler/circularReferenceInReturnType.ts +++ b/tests/cases/compiler/circularReferenceInReturnType.ts @@ -10,3 +10,13 @@ const res2 = fn2()(() => res2); declare function fn3(): (cb: (arg: T2) => any) => (a: T) => void; const res3 = fn3()(() => res3); + +// https://github.com/microsoft/TypeScript/issues/58616 + +function foo(arg: Parameters[0]) { + return arg; +} + +function bar(arg: string) { + return foo(arg); +}