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
4.4 regression: in operator does not narrow types in generic constraints when destructuringΒ #45762
typeX={a: string}|{b: string}// Works in 4.3.5 and 4.4.2functionfunc1<TextendsX>(value: T){if('a'invalue){consta=value.aconsole.log('a',a)}else{constb=value.bconsole.log('b',b)}}// Works in 4.3.5, not in 4.4.2functionfunc2<TextendsX>(value: T){if('a'invalue){const{ a }=value// Property 'a' does not exist on type 'X'.(2339)console.log('a',a)}else{const{ b }=value// Property 'b' does not exist on type 'X'.(2339)console.log('b',b)}}
π Actual behavior
Types are not narrowed when using destructuring assignment and the compiler throws error 2339.
π Expected behavior
Types are narrowed when using destructuring assignment.
Iβd start by making sure getNarrowableTypeForReference is returning X rather than T for the reference value, since thatβs what 4.3 added that makes this possible in the first place.
Iβd start by making sure getNarrowableTypeForReference is returning X rather than T
Andrew Branch (@andrewbranch) It seems this commit 0cc5c7b changed getNarrowableTypeForReference behavior to returning T rather than X for binding patterns.
Reacted by Gabriela Araujo Britto and Andrew Branch
Activity
andrewbranch commentedon Sep 7, 2021
Iβd start by making sure
getNarrowableTypeForReferenceis returningXrather thanTfor the referencevalue, since thatβs what 4.3 added that makes this possible in the first place.a-tarasyuk commentedon Sep 12, 2021
Andrew Branch (@andrewbranch) It seems this commit 0cc5c7b changed
getNarrowableTypeForReferencebehavior to returningTrather thanXfor binding patterns.andrewbranch commentedon Sep 13, 2021
I subsequently killed more contextual typing by binding patterns in #45719, not sure at a glance whether that will play a role here or not