microsoft / microsoft/TypeScript

nested destructured tuple value is `string | undefined` when second tuple item is not accessed

Offen
#42,969 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: flag: noUncheckedIndexedAccess
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Bug Report

When using noUncheckedIndexedAccess, the type inferred by my code is affected by whether I access the second item in a tuple or not.

🔎 Search Terms

tuple, undefined, type, omit, noUncheckedIndexedAccess, error, index, array, infer, nested

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noUncheckedIndexedAccess
  • I was unable to test this on prior versions because --noUncheckedIndexedAccess didn't exist before those versions
⏯ Playground Link

Playground link with relevant code

💻 Code
declare const fn: <T extends string[]>(t: T) => [T, string[]];

const [[x]] = fn(['1']);

console.log(x.toLowerCase());

It seems that the inferred type for fn is:

const fn: <string[]>(t: string[]) => [string[], string[]]

Since the first item is an array rather than a tuple, items in that array have type string | undefined.

If I change the assignment to be:

const [[x], []] = fn(['1']);

It gets inferred as:

const fn: <[string]>(t: [string]) => [[string], string[]]

Since the first item is a tuple rather than an array, items in that tuple have type string.

🙁 Actual behavior

x has type string | undefined

🙂 Expected behavior

x has type string.

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 dem verlinkten TypeScript Playground und reproduziere die beiden Destrukturierungsformen unter noUncheckedIndexedAccess. Verfolge die unterschiedlichen inferierten Typen, die im Bericht angezeigt werden; abgeschlossen ist die Aufgabe, wenn die erste Form x den Typ string gibt und dabei das erwartete Verhalten für die zweite Form erhalten bleibt.

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
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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