-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
Experimentation NeededSomeone needs to try this out to see what happensSomeone needs to try this out to see what happensSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
π Search Terms
destructuring object within destructured array, discriminated union
π Version & Regression Information
the error part appeared in every version I tried
β― Playground Link
π» 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
kykungz
Metadata
Metadata
Assignees
Labels
Experimentation NeededSomeone needs to try this out to see what happensSomeone needs to try this out to see what happensSuggestionAn idea for TypeScriptAn idea for TypeScript