microsoft / microsoft/TypeScript

Unions seem to sidestep `--noUncheckedIndexedAccess` and lose the possibility of undefined

Offen
#61,225 0 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔎 Search Terms

noUncheckedIndexedAccess, union, undefined

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about --noUncheckedIndexedAccess
⏯ Playground Link

Playground link

💻 Code
const nums: { [k: string]: number } = Math.random() < 0.5 ? { a: 1 } : { b: 2 };
const str = { a: "hello" }

// with --noUncheckedIndexedAccess on
const hmm = (Math.random() < 0.5 ? nums.a : str.a) // string | number | undefined
const wha = (Math.random() < 0.5 ? nums : str).a // string | number <-- 😕
🙁 Actual behavior

The type of wha is string | number, completely ignoring the possibility that it might be undefined, even though --noUncheckedIndexedAccess is enabled. Looks like unions of types with index signatures and known keys lose the --noUncheckedIndexedAccess behavior.

🙂 Expected behavior

wha should be of type string | number | undefined, just like hmm, since indexed access into a union should look like a union of indexed accesses.

Additional information about the issue

This is related to #50474. It’s also related to #47531, which had specifically to do with never[] and so the focus was on avoiding never[] as opposed to investigating what happened to the indexed access. ( https://github.com/microsoft/TypeScript/issues/47531#issuecomment-1229522810 )

Ran into this when looking at a Stack Overflow question

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 verknüpften TypeScript Playground-Reproduktion unter Verwendung von --noUncheckedIndexedAccess und vergleiche die beiden Indexed-Access-Ausdrücke im Issue. Das Issue nennt keine Repository-Datei und keinen Test; verfolge, wie Indexed Access für Unions aufgelöst wird, füge dann eine Abdeckung hinzu, die zeigt, dass der zweite Ausdruck undefined einschließt, und führe die relevanten Compiler-Tests aus.

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
42/100

Neue Issues direkt in Ihr Postfach

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