microsoft / microsoft/TypeScript

discriminated union type narrow when destructuring object within desturct array

Aperta
#55,664 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔎 Search Terms

destructuring object within destructured array, discriminated union

🕗 Version & Regression Information

the error part appeared in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?target=1&ts=5.2.2#code/C4TwDgpgBAYglgJwM7AJLAgWygXigbwCgopRIAuKAcgDNEUrioA3AQwBsBXCSlBOAHYBzQgF9ChMtADKEAMYB7AQBN0WXASZTKVJPKXLGJNlx5QBnTACMICMRKlQAKgAtEqjNjxES26sDcEQyYTbkorBQV2CFYBe0JFARQoVkoACnhkNE8oAB8oWUSPdXzXdzVMAEoAbQBdDTqJAHom0ghkgEZCVgA6GgUEAFFWORc0tPxScAgAGhYObihRStwAPk0SOBooNMccfeo6LKoVnxIoFqgFAGteYH5hJhJEpCiIHvYFITTQiEqmcTLZqtDDJABMhH6CB2L2ABCmkDmvyWV22rFOTC2Oz2B1o9GAJw250uNzuDxE5ygLzeHy+PwWfwB8QSSmSVko1Vg+IqeQK+hUPNKgWK2FqdQatWBbWSAGZIQMYay4ZzmFB6gptlYMZttj8eji8Hjjtria0bk8qayaZ9vswer9-iRAVLQXCACyEKx9AbDUbjaqTKRIhlLWorHDrM5Y3bTXC4o4ME0kS4CBRw0lQPiCIS8izWWy8iJvWIW6nRWm2hmOpZif6ES6uqAAVnl0LSsKgnMD02DphR6s1JujBsO+MJZ2TrVT6dumfu2dzlhs0PyReiJcpZfeNvppmrgKAA

💻 Code
type FirstItem = {
  type: 'first'
  value: string
}

type SecondItem = {
  type: 'second'
  value: number
}

type ThirdItem = {
  type: 'third'
  value: boolean
}

const a: (FirstItem | SecondItem | ThirdItem)[] = []

// test 1
a.forEach(({ type, value }) => {
  if (type === 'first') {
    // ok: string
    console.log(value)
  }
})

// test 2
for (const { type, value } of a) {
  if (type === 'first') {
    // ok: string
    console.log(value)
  }
}


const b: [ FirstItem | SecondItem | ThirdItem ][] = []

// test 3
for (const [ v ] of b) {
  if (v.type === 'first') {
    // ok
    console.log(v.value)
  }
}

// test 4
b.forEach(([{ type, value }]) => {
  if (type === 'first') {
    // not ok: string | number | boolean
    console.log(value)
  }
})

// test 5
for (const [ { type, value } ] of b) {
  if (type === 'first') {
    // not ok: string | number | boolean
    console.log(value)
  }
}
🙁 Actual behavior

the code above, test4 and test5 can not narrow to the string type like test1, test2 or test3

🙂 Expected behavior

I expected the value in the loop to infer successfully

Additional information about the issue

No response

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 il Playground Link e riproduci i test 4 e 5 dell’issue, confrontandoli con i test 1–3. Traccia il comportamento del controllo dei tipi per gli oggetti destrutturati all’interno di array destrutturati; è completato quando la variabile value viene ristretta a string quando type è 'first'.

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.