You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
discriminated union type narrow when destructuring object within desturct arrayΒ #55664
typeFirstItem={type: 'first'value: string}typeSecondItem={type: 'second'value: number}typeThirdItem={type: 'third'value: boolean}consta: (FirstItem|SecondItem|ThirdItem)[]=[]// test 1a.forEach(({ type, value })=>{if(type==='first'){// ok: stringconsole.log(value)}})// test 2for(const{ type, value }ofa){if(type==='first'){// ok: stringconsole.log(value)}}constb: [FirstItem|SecondItem|ThirdItem][]=[]// test 3for(const[v]ofb){if(v.type==='first'){// okconsole.log(v.value)}}// test 4b.forEach(([{ type, value }])=>{if(type==='first'){// not ok: string | number | booleanconsole.log(value)}})// test 5for(const[{ type, value }]ofb){if(type==='first'){// not ok: string | number | booleanconsole.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
Please edit the title to use the term "destructuring" instead of "desturcting" so that title searches can find it.
Looks like a missing feature as opposed to a bug, since #46266 only operates if the thing being destructured is a discriminated union, and [ FirstItem | SecondItem | ThirdItem ][] is not a discriminated union.
changed the title [-]discriminated union type narrow when desturcting object within desturct array[/-][+]discriminated union type narrow when destructuring object within desturct array[/+]on Sep 7, 2023
Joe Calzaretta (@jcalz) thank you. I agree this is more than a missing feature, after reading the linked issue. Type [ FirstItem | SecondItem | ThirdItem ], the first item of the array type, is a discriminated union, but it is nested. Destructuring the nested object(array) all at once or in a seperate way, when using forEach and for...of, the results are not the same. It seems a little complex.
Activity
jcalz commentedon Sep 7, 2023
Please edit the title to use the term "destructuring" instead of "desturcting" so that title searches can find it.
Looks like a missing feature as opposed to a bug, since #46266 only operates if the thing being destructured is a discriminated union, and
[ FirstItem | SecondItem | ThirdItem ][]is not a discriminated union.#46266 (comment)
[-]discriminated union type narrow when desturcting object within desturct array[/-][+]discriminated union type narrow when destructuring object within desturct array[/+]wusiquan commentedon Sep 7, 2023
Joe Calzaretta (@jcalz) thank you. I agree this is more than a missing feature, after reading the linked issue. Type
[ FirstItem | SecondItem | ThirdItem ], the first item of the array type, is a discriminated union, but it is nested. Destructuring the nested object(array) all at once or in a seperate way, when usingforEachandfor...of, the results are not the same. It seems a little complex.kykungz commentedon Oct 17, 2023
Related use case: TanStack/query#6162
mattfbacon commentedon Apr 9, 2024
Is this the same issue?