How to get type of property that's not defined for every union member?
type TypeA = { a: string }
type TypeB = { a: number; b: number }
type TypeC = { c: number }
type UnionType = TypeA | TypeB | TypeC
For example, property "a" in this sample union is of type string | number | undefined. How to get this type (during compile time, without run-time JS code)?
The closest I could get so far is this:
type PropertyA = (UnionType & { a: {} })['a'] // This gives me string | number | {}
But the type contains an extra {} that I couldn't get rid of.
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.