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
Type inference in switches over expressions pointing to nested properties does not work #41830
enumTYPES{ONE='one',TWO='two'}interfaceIOne{a: string,type: TYPES.ONEmeta: {type: TYPES.ONE}}interfaceITwo{b: string,type: TYPES.TWOmeta : {type: TYPES.TWO}}[].map((block:IOne|ITwo)=>{// This works.switch(block.type){caseTYPES.ONE:
block.abreak;caseTYPES.TWO:
block.bbreak;}// This does NOT work.switch(block.meta.type){caseTYPES.ONE:
block.abreak;caseTYPES.TWO:
block.bbreak;}});
Expected behavior:
The switch over block.meta.type should infer the same types as the switch over block.type.
Actual behavior:
The swich over block.meta.type errors the following and does not infer type correctly. The switch over block.type DOES infer type correctly.
Property 'a' does not exist on type 'IOne | ITwo'. Property 'a' does not exist on type 'ITwo'.
Property 'b' does not exist on type 'IOne | ITwo'. Property 'b' does not exist on type 'IOne'.
Note:
I'm sorry if this is obvious or at least an obvious duplicate. I've searched, I've asked on Gitter and Stack Overflow. I'm fairly new to Typescript and this is my first bug repport. I did my best.
Activity
RyanCavanaugh commentedon Dec 7, 2020
Duplicate #18758
u840903 commentedon Dec 7, 2020
Possible workaround using a combination of type guards and generics can be found here. https://stackoverflow.com/questions/65139900/when-does-typescript-type-inference-in-switch-statements-work