Last active
July 21, 2021 07:04
-
-
Save SephReed/46bd254b316152f38186d83109394791 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type UnionToIntersection<U> = ( | |
U extends any ? (k: U) => void : never | |
) extends (k: infer I) => void | |
? I | |
: never | |
type SubpropertyMerge<T> = ( | |
T extends object ? ( | |
T extends string | number | ((...args: any) => any) | symbol | boolean ? T | |
: { [K in keyof T]: SubpropertyMerge<T[K]> } | |
) : T | |
); | |
function smoosh<T extends object>(objs: T[]): SubpropertyMerge<UnionToIntersection<T>> { | |
let out = {} as any; | |
Object.entries(objs).forEach(([key, value]) => { | |
if (typeof value === "object" && out[key]) { | |
out[key] = smoosh([out[key], value]); | |
} else { | |
out[key] = value; | |
} | |
}) | |
return out; | |
} | |
// if you don't define the generic, this won't work | |
const eg = smoosh< | |
{ foo: {a: 1}} | |
| { foo: {b: 2}} | |
>([ | |
{ foo: {a: 1}}, | |
{ foo: {b: 2}} | |
]); | |
eg.foo.a | |
eg.foo.b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment