Is it possible for TypeScript to perform a union type assertion in this scenario? I am trying to use ab.a
, ab.b
, or ab.hasOwnProperty
to assert either type A
or type B
. How can I achieve this?
export interface A extends Object {
a: string;
}
export interface B extends Object {
b: number;
}
export type AorB = A | B;
function test(ab: AorB) {
// Can TypeScript automatically infer this?
if (ab.hasOwnProperty('a')) {
ab.a // type error
}
}