Repository: TypeScript
Issue: Generic Tuple Optional Parameter spread errors with Type instantiation is excessively deep and possibly infinite. ts(2589)
Labels: Possible Improvement

# Bug Report

```typescript
type StringableParameters<TArgs> 
  = TArgs extends [optionalHead?:(infer OptionalArg), ...optionalRest:infer OptionalRest] //optionalHead? <- this question mark is what's causing error
    ? [(OptionalArg|string)?, ...StringableParameters<OptionalRest>] //ts(2589)
    : []

type Test = StringableParameters<[1,2?,3?]>
// type Test = [(string | 1)?, (string | 2)?, (string | 3)?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, any?, ...any[]]
```

That question mark `TArgs extends [optionalHead?:(infer OptionalArg)` allowing parameters to be optional is leading to the infinite nesting with ts2589 error. If I remove that question mark - it compiles, but omit the optional parameters of the tuple

```typescript
type StringableParameters<TArgs> = TArgs extends [optionalHead:(infer OptionalArg), ...optionalRest:infer OptionalRest]
? [(OptionalArg|string), ...StringableParameters<OptionalRest>]
: []
type Test = StringableParameters<[1, 2?, 3?]>

//type Test = [string | 1] - optional parameters omitted
```

### 🔎 Search Terms

Type instantiation is excessively deep and possibly infinite. ts(2589)
Generic Tuple extends Optional argument

### 🕗 Version & Regression Information

Typescript v5, v4.7.4,...
### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior.
  This should have the same code as the code snippet below, and use whichever settings are relevant to your report.

  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=4.7.4#code/C4TwDgpgBAysBOBLAdgcwIYCMA2EAK686AthMBPAM4A8AKgILyqUB8UAsAFBRQC8UDJpSgQAHuWQATYQG0A9mGCI5ydNgASEdJID8ALgAUKAGYUoAeUXLV2RqgCUAGigA6NwqUq1AJQiVgeiZmlp42vv4AulAA9NEe1mqa2jpQ1AC0UMAAFojCAI4Arn6hUMSEANZQuVAA7lnowADkwgDG6AWUKKgi8PBy8Fw8PCkyBiEJtkwAPv5IaPY6zm4ucHMYOPiEJGQUNONe2OHALFGxwJQGAEwArAAcAJz2g0N6UDIRXFygkALF+zZ8WAILpYXAEIikchUagyACMjkuiwAzDoIiwuNEAFSZcDQWh-KwHQGjWZdKBTKCw8lQApSCDGFAQSQLZwGUloamXam0yT0xnMxZQNnAjkUpHcukM5BMlk05DlZByGrIQW0hVKlXONWK5Wq+U6zVy9W6rX6jWCrja82m42Gq0mo0GvW251Om1ux3Wz0O+12s0+-1+l3ur2+13WwZhkMB4PeoMeqNx8MxhOB5Px0Np6MZlMR7iJgtZpPZ9OlpZuKAANlK-Wgy0F6GQIFRXEx0U+nG+0FWII24O2UMoADk5P81HQ7KwONxAYJmCJxBApLJ4gckpJDEF4BZCTY7E5XO5dz5ioFkKZt2PDsVTnFjxotLpUhlstVCsVrKUKlVhHUGs0oDaDoyQoPoBhnYY3jGe87BmEUHHLFZ4NBTYIR2KgRyvagryOE4Ymic4rjuR5nh4V53g7Ltfn8TD70BHs0BQ-tIV2WjQnHOEEWRVF0U4M5cWo4A2ImYl2W6ClYQ+TggA)

### 💻 Code

```ts
type StringableParameters<TArgs> 
  = TArgs extends [optionalHead?:(infer OptionalArg), ...optionalRest:infer OptionalRest] //optionalHead? <- this question mark is what's causing error
    ? [(OptionalArg|string)?, ...StringableParameters<OptionalRest>] //ts(2589)
    : []

type TestOptional = StringableParameters<[1,2?,3?]>
/* type TestOptional = [(string | 1 | undefined)?, (string | 2 | undefined)?, (string | 3 | undefined)?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, 
unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?,
 unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?,
 unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, unknown?, ... 6 more ...?, any?]
*/

type StringableParametersNoOptional<TArgs> 
  = TArgs extends [optionalHead:(infer OptionalArg), ...optionalRest:infer OptionalRest] //optionalHead? <- this question mark is what's causing error
    ? [(OptionalArg|string), ...StringableParametersNoOptional<OptionalRest>] //ts(2589)
    : []

type TestNoOptional = StringableParametersNoOptional<[1,2?,3?]>
//type TestNoOptional = [string | 1]
```

### 🙁 Actual behavior

Type instantiation is excessively deep and possibly infinite.(2589) and if I use ts-ignore, the TestOptional type has excessive amount of unknown optional parameters on the tail

### 🙂 Expected behavior

```ts
type TestOptional = [(string | 1 | undefined)?, (string | 2 | undefined)?, (string | 3 | undefined)?]
```