microsoft / microsoft/TypeScript

Object access reports error after upgrading to TypeScript 5.5

Aperta
#59,185 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔎 Search Terms

"noUncheckedIndexedAccess", "Object is possibly 'undefined'."

🕗 Version & Regression Information
  • This changed between versions 5.4.5 and 5.5.3
⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.5.3#code/DYUwLgBAzmBOBc04EsB2BzCAfCBXVAJiAGZogEDcAUMsRABQCyAhmABYB0szhA9gLb0AlBAA8EAAwcAjCIDeVCElgQAvBABEzDVQC+VKgGNeqGBGaHDiORADaqZvxCIYsNOgC6iVLn4AjEBVdNQg5XWpaBlcIAEJ1H2BgeUVzS1tXDwgAfiz1CWolC0N0uA8AajLqfSA

💻 Code
// @strict: true
let str: string | undefined;
if (Math.random() < 0.1) {
  str = "a"
}

const acc: { [name: string]: number } = {};
if (str != null) {
  acc[str] ??= 0;
  acc[str]++; // In TS 5.5: Object is possibly 'undefined'. (2532)
}
🙁 Actual behavior

In TS 5.5.x, with noUncheckedIndexedAccess enabled, acc[str]++; reports an error "Object is possibly 'undefined'. (2532)".

🙂 Expected behavior

In TS 5.4.x and before, no errors were reported.

Additional information about the issue

I admittedly didn't do a very deep dive on the root cause here, but wanted to file an issue because intuitively it felt unexpected when upgrading to TS 5.5 that the above code now fails type-checking. Apologies if I missed an existing issue for this or something in the release notes!

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 dal repro collegato di TypeScript Playground e confronta le relative diagnostiche con 5.4.5 e 5.5.3, concentrandoti su noUncheckedIndexedAccess, nullish assignment e indexed access. Traccia il comportamento del checker responsabile della regressione e aggiungi un test di regressione; il lavoro è completato quando l'esempio non segnala più la diagnostica errata, mentre i controlli di sicurezza pertinenti rimangono intatti.

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.