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
interfaceA<T>{variant: 'a'value: T}interfaceB<T>{variant: 'b'value: Array<T>}typeAB<T>=A<T>|B<T>functionprintValue<T>(t: T): void{console.log(t)}functionprintValueList<T>(t: Array<T>): void{console.log(t)}functionunrefined1<T>(ab: AB<T>): void{const{ variant, value }=abif(variant==='a'){printValue<T>(value)}else{printValueList<T>(value)}}functionunrefined2<T>(ab: AB<T>): void{const{ variant }=abif(variant==='a'){const{ value }=abprintValue<T>(value)}else{const{ value }=abprintValueList<T>(value)}}functionrefined<T>(ab: AB<T>): void{if(ab.variant==='a'){const{ value }=abprintValue<T>(value)}else{const{ value }=abprintValueList<T>(value)}}
Expected behavior:
A destructured shared field of a discriminated union should be refined inside of a block that refines the discriminated union itself. I think the code example explains what I'd expect to happen better than I can in words.
Actual behavior:
Variables that are destructured prior to the union being refined are still stuck as unions within a block where the discriminated union is refined.
Additional comments:
For what it's worth, the code example above is still valid without a type parameter i.e. a concrete type where T would be
I can't think of any scenario where the destructured value would not be correctly refined if this behavior were implemented.
Related Issues:
I couldn't find any open issues regarding object destructuring and type guards, but these were the only two closest to the issue I'm seeing. #18840 - Type guards on variable break refinements on its properties #20504 - Nested if statement invalidates type guards
The text was updated successfully, but these errors were encountered:
TypeScript Version:
3.8.0-dev.20191121
Search Terms:
Code
Expected behavior:
A destructured shared field of a discriminated union should be refined inside of a block that refines the discriminated union itself. I think the code example explains what I'd expect to happen better than I can in words.
Actual behavior:
Variables that are destructured prior to the union being refined are still stuck as unions within a block where the discriminated union is refined.
Additional comments:
T
would bePlayground Link
Related Issues:
I couldn't find any open issues regarding object destructuring and type guards, but these were the only two closest to the issue I'm seeing.
#18840 - Type guards on variable break refinements on its properties
#20504 - Nested if statement invalidates type guards
The text was updated successfully, but these errors were encountered: