You can do it with a combination of:
interface Example {
a: string;
b: number;
}
type SingleProperty<T, K extends keyof T> = K extends any ? {[Prop in K]: T[Prop]} : never
type UnionOfProperties<T> = { [K in keyof T]: SingleProperty<T, K> }[keyof T];
type ExamplePropertiesUnion = UnionOfProperties<Example>
This returns expected:
type ExamplePropertiesUnion = {
a: string;
} | {
b: number;
}
While the above is correct, TS will allow the following
var t: ExamplePropertiesUnion = {a: "", b: 42}
Which is NOT what we usually want:
Here below is the variant for a stricter type checking
type FixTsUnion<T, K extends keyof T> = {[Prop in keyof T]?: Prop extends K ? T[Prop]: never}
type oneOf<T> = { [K in keyof T]: Pick<T, K> & FixTsUnion<T, K>}[keyof T];
// ok
var z1: oneOf<Example> = { a: "" };
// ok
var z2: oneOf<Example> = { b: 5 };
// error
var z3: oneOf<Example> = { a: "", b: 34 };
// error
var z4: oneOf<Example> = { };
Try it here
See questions: