I'm trying to convert a union type to an intersection type:
type UnionToIntersection<U> = // unknown code
type test = UnionToIntersection<{a: 1} | { b: 2}>
// typeof test should be {a: 1} & { b: 2}, an impossible type
This is just one step in a larger type function which will then merge the properties of {a:1}
and {b:2}
to make {a:1, b:2}
. But that's a later step.
First step is I need to convert my union to an intersection. How can I do that?
For those wondering, it will then go into this:
export type SubpropertyMerge<T> = (
T extends (...args: infer A) => infer R ? (
(...args: A) => R
): (
T extends object ? (
T extends string | number | ((...args: any) => any) | symbol | boolean ? T
: { [K in keyof T]: SubpropertyMerge<T[K]> }
) : T
)
);
// SubpropertyMerge<{a: 1} & { b: 2}> === {a:1, b:2}