TypeScript understands Type Guards as a way to decompose union types. There are several ways to use this.
If Fooor Baris a class, you can use instanceof:
if (a instanceof Foo) { a.doFooThing(); }
If these are interfaces, you can write a custom protection type:
function isFoo(f: any): f is Foo {
// return true or false, depending....
}
if (isFoo(a)) {
a.doFooThing();
} else {
a.doBarThing();
}
typeof a === 'string' (string, number boolean)