microsoft / microsoft/TypeScript

Object destructuring with default empty object generates wrong type

Aperta
#41,189 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Type Inference Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

TypeScript Version: 4.0.3

Search Terms:

destructuring
object destructuring
object destructuring default

Code

const random = () =>
	new Date().getTime() % 2 === 0 ? { prop1: 1, prop2: 2, prop3: 3 } : undefined;
const obj = random();
// the type of obj is { prop1: number; prop2: number; prop3: number; } | undefined

const test = { testProp: obj };
const { testProp } = test;
// the type of testProp is { prop1: number; prop2: number; prop3: number; } | undefined
const { testProp: testProp2 = {} } = test;
// the type of testProp2 is: {}

const { prop1 } = obj ?? {}; // works
const { prop2 } = testProp ?? {}; // works
const { prop3 } = testProp2; // error

Expected behavior:

const { prop3 } = testProp2 should work.

Actual behavior:

const { prop3 } = testProp2 returns the error: Property 'prop3' does not exist on type '{}'. ts(2339).

Additional information:

const { prop3 } = testProp2 should work, just like const { prop2 } = testProp ?? {}, because the only difference is that the default {} was assigned during the object destructuring to get testProp2 while, for testProp, the default was assigned during the destructuring of the object itself.

If obj type is Type | undefined, I would expect testProp2 type to be Partial<Type>, as long as Type is not a primitive, but an object (which it is in the above code, so testProp2 should be Partial<{ prop1: number; prop2: number; prop3: number; }>).

Related Issues:

https://github.com/microsoft/TypeScript/issues/41080 (similar, but not the same, already fixed)

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 la riproduzione in TypeScript 4.0.3 presente nell'issue e confronta le due forme di destrutturazione, quindi esamina l'issue correlata #41080 per il contesto. Il lavoro è completato quando testProp2, a cui è assegnato un valore predefinito, conserva le proprietà dell'oggetto, così la destrutturazione di prop3 non segnala più un errore.

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.