microsoft / microsoft/TypeScript

Spread operator mixed with destructuring assignment resulting in unsoundness

Aperta
#54,402 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Domain: flag: noUncheckedIndexedAccess Help Wanted Possible Improvement
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Bug Report

This might have been covered before but I am not sure how to best search for it, apologies if it is a duplicate - when destructuring assignment is used with a spread operator (see example below) TS seems to ignore the possibility that the variable could be assigned undefined.

🔎 Search Terms

spread, destructure, destructuring

🕗 Version & Regression Information
  • This is the behavior in every version I tried (since 4.1.5 which is the oldest version on the playground with noUncheckedIndexedAccess available)
⏯ Playground Link

Playground link with relevant code

💻 Code
const [a] = [...(false ? (["a"] as const) : [])];
//     ^?
const b = false ? (["a"] as const) : [];
//    ^?
const c = [...b];
//    ^?
const [d] = c;
//     ^?

const [a_prime] = [...(false ? (["a"] as const) : [] as const)];
//     ^?
🙁 Actual behavior

a is typed as "a", while d and a_prime are typed as "a" | undefined". Note this only happens when noUncheckedIndexedAccess is enabled, otherwise all of them are typed as "a".

Note that d is the unrolled version of a which makes it more surprising that TS is typing them differently.

🙂 Expected behavior

They should all be typed "a" | undefined.

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 con l'esempio collegato di TypeScript Playground e confronta i tipi inferiti di a, d e a_prime con noUncheckedIndexedAccess abilitato. Traccia la gestione da parte del compilatore del destructuring degli array e delle espressioni spread; il lavoro è completato quando tutti e tre i casi inferiscono coerentemente "a" | undefined e viene aggiunta la copertura di regressione per l'esempio segnalato.

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.