microsoft / microsoft/TypeScript

discriminated union type narrow when destructuring object within desturct array

Open
#55,664 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Playground Link and reproduce tests 4 and 5 from the issue, comparing them with tests 1–3. Trace the type-checking behavior for destructured objects inside destructured arrays; done means the value variable narrows to string when type is 'first'.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.