Repository: TypeScript
Issue: Infer array as tuple when used as rest argument in a complex expression
Labels: Help Wanted,Possible Improvement

### 🔍 Search Terms

"infer rest array type"
"infer rest array args"
"infer rest array tuple"
"spread argument array expression "

### ✅ Viability Checklist

- [X] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [X] This wouldn't change the runtime behavior of existing JavaScript code
- [X] This could be implemented without emitting different JS based on the types of the expressions
- [X] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [X] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [X] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

Given the following function
```ts
function foo (a: number, b: string, c: boolean) {}
```

TypeScript already supports inferring that an array used as rest argument should be considered as a tuple:

```ts
foo (1, ...(["a", true])) // Works
```

But if you add some condition to the mix, type inference breaks:

```ts
const someCondition = true

foo (1, ...(someCondition ? ["a", true] : ['b', false])) // A spread argument must either have a tuple type or be passed to a rest parameter.(2556)
```

My suggestion it to add support for inlined array inside complex expression to be used as spread args.

[Playground link](https://www.typescriptlang.org/play/?jsx=0#code/MYewdgzgLgBBIFsCmBhcATAllT4YF4YoAnAVyQFgAoAM1LGBzxpBBgAoBDALhjFIQAjJMQA0MQb2jFMYAObjgvQawA2STmACUMAN4BfatRZt2ARnEA6a+wDaAIk73xJcgF0tOgPReYAdRBiAGsII1pWDgsYa0t2eGQ0MCwmMBgAfhgHJxcyJDcYXlsAckEi8RpOVQg8zxgfGABBOAAHYg10GE5iOQEkMFgEUmgYJGwACxEYMc4ANyROolJm9SIAT2b5wIl55s4Iao6oNk4YNuHd4k5kKBFYgCYAVgeANi0wk0irG3jUDGxcVIZLLOIi5fIFTIlMowCpVGqdCCZaSyBQSNQaMAeOq+ALBUJUIA)

### 📃 Motivating Example

I came accros this issue while designing a database agnostic module based on kysely.

I used to write stuff like so:

```ts

const myTableWithId =
  dialect === 'pg'
    ? db.schema
        .createTable('my_table')
        .addColumn('id', 'bigserial', (col) => col.primaryKey())
    : db.schema
        .createTable('my_table')
        .addColumn('id', 'bigint', (col) => col.autoIncrement().primaryKey())

await myTableWithId
  .addColumn('my_col', 'varchar', (col) => col.notNullable())
  .execute()

```

But this breaks the linear style of the definition. The following notation is more natural:

```ts
await db.schema
  .createTable('oauth_device')
  .addColumn(
    'id',
    ...(dialect === 'pg'
      ? ['bigserial', (col) => col.primaryKey()]
      : ['bigint', (col) => col.autoIncrement().primaryKey()])
  )
  .addColumn('my_col', 'varchar', (col) => col.notNullable())
  .execute()
```

It currently work but requires additional typecasting (see playground link before)

### 💻 Use Cases

1. What do you want to use this for? Be able to write more readable code
3. What shortcomings exist with current approaches? Unnecessarily more verbose
4. What workarounds are you using in the meantime? Declare the type of the spread tuple and cast every value to that type