microsoft / microsoft/TypeScript

Equivalent Tuple with Rest types not duck-typing properly

Aperta
#52,496 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

A tuple type with a rest element doesn't duck-type properly to the similar tuple type, although they are conceptually identical, causing a ts2345 error to show up when trying to use one with the other.

The type I tested was a type defining a non-empty array. It looks like there's a bug because the location of the "non-empty item" in the definition matters for typescript, although there shouldn't be any difference in the kind of arrays that are accepted for each type.

🔎 Search Terms
  • tuple
  • rest
🕗 Version & Regression Information

Since 4.2 where rest params where allowed a the start of a tuple too

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about tuple
⏯ Playground Link

Playground link with relevant code

💻 Code
type FirstItem<T> = [T, ...T[]];
type LastItem<T> = [...T[], T];

const f0: FirstItem<number> = []
const l0: LastItem<number> = []

const f1: FirstItem<number> = [1]
const l1: LastItem<number> = [1]

const f2: FirstItem<number> = [1, 2]
const l2: LastItem<number> = [1, 2]

function foo(x: FirstItem<number>): void {
    // DO NOTHING
}

foo(f2) // OK
foo(l2) // ts2345

const x: number[] = [2, 3, 4]

foo([1, ...x]) // OK
foo([...x, 5]) // ts2345
🙁 Actual behavior
  • ts2345 is shown
🙂 Expected behavior
  • the two types should be considered equivalent and duck-typeable.

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 il link a Playground e con l'esempio ridotto in TypeScript che confronta FirstItem e LastItem. Traccia l'assegnabilità degli elementi rest delle tuple e la diagnostica ts2345 per foo(l2) e foo([...x, 5]). Il lavoro è completo quando i tipi di tupla equivalenti non vuoti vengono accettati in modo intercambiabile senza regressioni nei controlli delle tuple esistenti.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
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.