microsoft / microsoft/TypeScript

discriminated union type narrow when destructuring object within desturct array

Abierto
#55,664 4 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experimentation Needed Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

🔎 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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con el Playground Link y reproduce las pruebas 4 y 5 del issue, comparándolas con las pruebas 1–3. Traza el comportamiento de la comprobación de tipos para objetos desestructurados dentro de arrays desestructurados; se considera completado cuando la variable value se reduce a string cuando type es 'first'.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.