Repository: TypeScript
Issue: Tail-Recursion Elimination on Conditional Types
Labels: Suggestion,Help Wanted,Experience Enhancement

# Bug Report

<!--
  Please fill in each section completely. Thank you!
-->

### 🔎 Search Terms

recursion，instantiation，Tail-Recursion

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
- This is the behavior in every version I tried

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?ts=5.0.4#code/FAFwngDgpgBAgjAvDA3jAhgLhgZxAJwEsA7AcwBoYAjbYgVwFsqp8YBfUSWAISVQ1qNm+SjVwESpdsE7QYAVWKEA9sQAqACyhquAeQBmmqAGV0DKAGkoYADwAlSgAkAfH10NCIe5QDW15fowLjAAZPwA2j4wJDB+YAFBALrYUVAAHiBQxAAmOLH+gXYwAPwwdpGJMAA+QRUw2I51HLKwAMbKDBB0mTZqrshokdHE+fGBaskwak0y4HL6ysq9MOmZOXnKVABWUK0g4YmU+FA4dAA2IHwobP1TKxlZuTDhJPosMFro2fdrT5s7e0oADoQa93sc8D9Hhttrt9olEsAYMiSjAFksISBKIoVOotDpoAYjKZzFZbBDzliPlAvs5XNgKRcZAB6ZnI9LQPYwZRROawTKQgZYPBEMhVehMFjkGgiyTioQsDh8mACy7IdqdbpQGzomzhOCUbjkRJ0llsgDqWhGIC0aMWMDOWVINuieVIx3QmVYNvQIwAzH7kUYYIzLoQ8gB3fCqUhAlVcFUnEAARiuWF9YGlmAzSoTqtT6o6XR6uv1hsoBpgRvg5ZrVYrtcr1abDfrdebjc7bZb7dbHe7hpNzhkQA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
type A = { a: string, b: number }
type B = { a: number, b: string }

type UnionTheTypeOfTheSameKey<R, H> = Omit<R, keyof H> & { [k in keyof H]: k extends keyof R ? R[k] | H[k] : H[k] }

type compute<T> = { [k in keyof T]: T[k] }

type foo<T extends object[], result = {}> = T extends [infer head extends object, ...infer rest extends object[]]
    ? foo<rest, UnionTheTypeOfTheSameKey<result, head>> : result

//  expect ok type test = {a:string|number,b:string|number}
type test = compute<foo<[A, B,]>>

// When the foo length is greater than 33  The result is wrong. type test1 = {a:any,b:any}
type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>
```

### 🙁 Actual behavior

<!-- What happened, and why it was wrong -->
// When the foo length is greater than 33  The expected is wrong. type test1 = {a:any,b:any}
type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>

### 🙂 Expected behavior

// When the foo length is Less than 999  expected test1 =  {a:string|number,b:string|number}
type test1 = compute<foo<[A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B, A, B]>>