tsafe
Search
⌃K
Links

UnionToIntersection

type A = { foo: string; };
type B = { bar: number; };
​
type Got = UnionToIntersection<A | B>;
type Expected = A & B;
​
assert<Equals<Got,Expected>>();
Example:
import type { UnionToIntersection } from "tsafe/UnionToIntersection";
​
const o1= {
"p1": { "a": "foo" },
"p2": { "b": "foo", "c": "foo" }
};
​
const o2: UnionToIntersection<(typeof o1)[keyof typeof o1]> = {} as any;
​
objectKeys(o1).forEach(key=> Object.assign(o2, o1[key]));
​
//o2 is of type { a: string; b: string; c: string; }
Credit goes to jcalz for this type, see SO answer.
Last modified 2mo ago