microsoft / microsoft/TypeScript

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

Offen
#42,757 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Docs
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der verlinkten TypeScript Playground-Reproduktion und dem Tupeltyp-Beispiel, das ein unbeschränktes Rest-Element vor einem boolean zeigt. Verfolge die Validierung von Rest-Elementen für Tupeltypen im Compiler und bestätige anschließend, dass das dokumentierte TypeScript 4.0-Beispiel akzeptiert wird und dass sich das daraus resultierende unbeschränkte Tupel wie erwartet verhält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.