microsoft / microsoft/TypeScript

Unbounded arrays cannot be used as rest elements in a tuple type except at the end

Aperta
#42,757 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug Report

🔎 Search Terms

A rest element must be last in a tuple type.

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about rest elements and tuples.

Specifically, I tried it with 4.1.5 right now. Note that 4.0.5 behaved a bit differently (but still buggy), see below.

Note that the behavior is normal in versions ≤4.0, since that’s when non-final rest elements were allowed in tuples.

⏯ Playground Link

Playground link with relevant code

💻 Code
type Strings = [string, string];
type Numbers = number[];

type Unbounded = [...Strings, ...Numbers, boolean]; 
🙁 Actual behavior

The ...Numbers part generates an error: TS1256: A rest element must be last in a tuple type.

This exact code appears in TypeScript 4.0 release notes, thus presumably should work. (Search for “the resulting type becomes unbounded”.)

Note: I encountered something similar with 4.0.5 earlier. There, the non-final unbounded rest elements worked, but only via a type alias. They didn’t work with literal arrays in the tuple definition:

type Numbers = number[];

type Unbounded1 = [boolean, ...number[]]; // OK
type Unbounded2 = [...Numbers, boolean]; // OK
type Unbounded3 = [...number[], boolean]; // NOK

Playground Link for TS 4.0.5

🙂 Expected behavior

The feature should work as documented. TypeScript should generate unbounded tuples type that work the “obvious” way.

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 dalla riproduzione collegata in TypeScript Playground e dall'esempio di tipo tupla che mostra un elemento rest senza limite prima di un boolean. Traccia la validazione degli elementi rest del compilatore per i tipi tupla, quindi conferma che l'esempio documentato di TypeScript 4.0 venga accettato e che la tupla senza limite risultante si comporti come previsto.

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
Specificata chiaramente
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.