Repository: TypeScript
Issue: Trying to flatten an array with unknown depth causes a type error
Labels: Needs Investigation

# Bug Report

Trying to flatten an array with unknown depth causes a type error.

### 🔎 Search Terms

<!--
  What search terms did you use when trying to find an existing bug report?
  List them here so people in the future can find this one more easily.
-->
 * `flat`
 * `Infinity`

### 🕗 Version & Regression Information

- Errors in version 4.2.4
- This changed between versions 3.6.2 and 3.7.5 (but probably just because the type definitions aren't supported prior)

The PR https://github.com/microsoft/TypeScript/pull/32131 provides a definition for `FlatArray` that has this issue.

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/play?target=99&ts=4.3.2&filetype=ts#code/PTAEDMEsCcGcBdQBMCmUB2LQENSYSkqADaQKgDuk8AFjtAEbXTbQCeyKADrQFDxsuWAHIoCSADwAVAHygAvKAAUU0AB9Qo8dJkBKANoBdANy9eAYwD26cq2gAuTWPiEJ6AK4BbBimhzF+gCMADT6AEyhAMyGMaa8ICSW5tjExGzBoLRkoEyI4NiQxLAQltCgnlhKAG6+sJDWoAAsAHRhzY268cBdlNR02OigvtCloADkAETQKObu0BNjoNnT4L4o6OaEyDAz8Gmgo5DoSDvmexxHS-DFlhSDAkI46OiW8Njw9ejNFtbk07CODzeXxGBT0aDNcDEd5KACS6Aw1DYujiCTYlncoGSgyQlhwoAAju5IOYANaUUqk1gY444YrgSypW4An42RD-MKArw+aCgxRKOx0p5sIy6SHQ+BwhFHJEoswJBjuPKjWCWCqgabYVWDBq0LBSQQoADK5mgkB4oC40LYAHMRu5aVlirixOgxnkCsQMorEOjMdihp53BKsNQegwOO46ugbZkaFgGUyqDHOIiPrqaO8lsUUETIFUUutEPA8XqDphLSMqpBUERLmWEAMkKwiKQGCx2PYejR4PAuACQDa+u4GM0rJ5gJ4SSNVeB4MADUITWaeMAGMRLAxgEgABwoACc4AAbPuAKyBfcX7CRRoAdgYDECp9PDH3kR32FPAAYv0fwPu0GwB9b1vHdgDbcDIFHMQwi-C9mjsbA2GaJBmmuABiAAZMJAn4Q1QAAMQlABBaAWDYMIJFI6AMgAEW4WghgADxcY5iiBHl-FAABvXhQH40AJlxTAJkcajgj4gSphmOZRNAajmNYpBigAJRQbBhLSaikIkI5VjKeFMGgaiZEkgSBIAfkIkiyKQyjDN8cTQH0ABaEJQC-DJ3IiUBIgyRoMlPDIjwyW8Mh3DJ9y8zzQECdzAh8wI-NigLYqC2KQtisLYoi2KotAWDDH0eieBoQxTPM8yxLI3gAF9ioYugUBY9ZlNANzQCsoTrBQCZQEcaTZnmEx5TAZIozEfEHiwYZSkcSZpiGhZsw1NA1g2LYTkW84DjKI4tt2fZLmoG47kyfCBheN50y+PDHikZwiPeHQwSe+BtIoiQtBcSRZAyabLHAUB4TTNgZCAA)

### 💻 Code

```ts
// first define a nested list with arbitrary depth
type Nested<T> = (T | Nested<T>)[];

const arr: Nested<number> = [1,[2,[3]]];

// locally, this bit fails for me (version 4.2.4)
//
// with an error '"recur"' is referenced directly or indirectly in its own type annotation.
const res: number[] = arr.flat(Infinity);

// you can do a quick workaround as follows:
const res2: number[] = (arr as any[]).flat(Infinity);

// but for some reason on the TypeScript playground this doesn't fail, but you can emulate it
// by using the following definition that is equivalent to the one provided in the standard library:
// https://github.com/microsoft/TypeScript/blob/d8e9f6951919a347bb155b938a5006f9efabb778/lib/lib.es2019.array.d.ts#L21
type FlatArray2<Arr, Depth extends number> = {
    "done": Arr,
    "recur": Arr extends ReadonlyArray<infer InnerArr>
        ? FlatArray2<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]>
        : Arr
}[Depth extends -1 ? "done" : "recur"];

// causes a type error: '"recur"' is referenced directly or indirectly in its own type annotation.
type TestFlat<T> = FlatArray2<Nested<T>, typeof Infinity>
```

### 🙁 Actual behavior

The code errors with `'"recur"' is referenced directly or indirectly in its own type annotation.`

### 🙂 Expected behavior

The code to pass typechecking.