-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 4.1.2, 4.2.0-dev.20201205
Search Terms: type inference switch statement
Code
enum TYPES {
ONE = 'one',
TWO = 'two'
}
interface IOne {
a: string,
type: TYPES.ONE
meta: {
type: TYPES.ONE
}
}
interface ITwo {
b: string,
type: TYPES.TWO
meta : {
type: TYPES.TWO
}
}
[].map((block:IOne | ITwo) => {
// This works.
switch (block.type) {
case TYPES.ONE:
block.a
break;
case TYPES.TWO:
block.b
break;
}
// This does NOT work.
switch (block.meta.type) {
case TYPES.ONE:
block.a
break;
case TYPES.TWO:
block.b
break;
}
});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'.
Related Issues:
#30763
#30557
#30593
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.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created