Why does type inference work in example A but not in B? The only difference is the position of the type string. block.type vs block.meta.type. A compiles, and infers type and B results in.
// Example B errors
Property 'a' does not exist on type 'Block'. Property 'a' does not exist on type 'ITwo'.
Property 'b' does not exist on type 'Block'. Property 'b' does not exist on type 'IOne'.
How do I get B to compile, and infer correctly, without changing the data structure of IOne or ITwo?
Example A
enum TYPE { ONE = 'one', TWO = 'two'}
interface IOne {
a: string,
type: TYPE.ONE
}
interface ITwo {
b: string,
type: TYPE.TWO
}
type Block = IOne | ITwo
[].map((block:Block) => {
switch (block.type) {
case TYPE.ONE:
block.a
break;
case TYPE.TWO:
block.b
break;
}
});
Example B
enum TYPE { ONE = 'one', TWO = 'two'}
interface IOne {
a: string,
meta : {
type: TYPE.ONE
}
}
interface ITwo {
b: string,
meta : {
type: TYPE.TWO
}
}
type Block = IOne | ITwo
[].map((block:Block) => {
switch (block.meta.type) {
case TYPE.ONE:
block.a
break;
case TYPE.TWO:
block.b
break;
}
});
Thanks in advance, J.