I am currently attempting to define two distinct types that exhibit the following structure:
type A<T> = { message: string, data: T };
type B<T> = { age: number, properties: T };
type C<T> = A<T> | B<T>;
const x = {} as unknown as C<number>;
if (x.message) { // <--- HERE
// 'x' should be of type A
}
Despite my efforts, I am not achieving the desired type inference/narrowing in this scenario. Is it possible to make this construction work? If so, where might I be making a mistake?
Check out the Typescript Playground for experimentation and troubleshooting: Typescript Playground