microsoft / microsoft/TypeScript

Infer array as tuple when used as rest argument in a complex expression

Aperta
#56,765 3 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: check: Type Inference Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

🔍 Search Terms

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

✅ Viability Checklist
⭐ Suggestion

Given the following function

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:

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

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

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

📃 Motivating Example

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

I used to write stuff like so:


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:

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
  2. What shortcomings exist with current approaches? Unnecessarily more verbose
  3. What workarounds are you using in the meantime? Declare the type of the spread tuple and cast every value to that type

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con gli esempi collegati di TypeScript Playground e confronta il tuple spread diretto con il conditional spread nell’issue. Il lavoro è completato quando un’inline conditional array expression può essere usata come spread arguments per le funzioni tipizzate mostrate senza type assertions, mentre il comportamento di JavaScript a runtime rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.