Discriminated Union of Generic type
I'd like to be able to use union discrimination with a generic. However, it doesn't seem to be working:
Example code:
interface Foo{
type: 'foo';
fooProp: string
}
interface Bar{
type: 'bar'
barProp: number
}
interface GenericThing {
item: T;
}
let func = (genericThing: GenericThing) => {
if (genericThing.item.type === 'foo') {
genericThing.item.fooProp; // this works, but type of genericThing is still GenericThing
let fooThing = genericThing;
fooThing.item.fooProp; //error!
}
}
I was hoping that TypeScript would recognize that since I discriminated on the generic item property, that genericThing must be GenericThing.
Isn't this supported?
Also, kind of weird that after assignment, fooThing.item loses its discrimination.