Skip to content

Instantly share code, notes, and snippets.

@forivall
Last active December 3, 2019 20:44
Show Gist options
  • Save forivall/5ffe686c102614312c03394e3c549738 to your computer and use it in GitHub Desktop.
Save forivall/5ffe686c102614312c03394e3c549738 to your computer and use it in GitHub Desktop.
export type Deintersect<T> = Compute<
OptionalizeUndefined<
{
[K in keyof UnionToIntersection<T>]: T extends { [_ in K]: any }
? T[K]
: undefined;
}
>
>;
type Compute<A> = {
[K in keyof A]: A[K];
} & {};
type OptionalizeUndefined<T> = {
[K in UndefKeys<T>]?: NonUndef<T[K]>;
} &
{ [K in Exclude<keyof T, UndefKeys<T>>]: T[K] };
type NonUndef<T> = T extends undefined ? never : T;
type UndefKeys<T> = Exclude<
{
[K in keyof T]: T extends Record<K, NonUndef<T[K]>> ? never : K;
}[keyof T],
undefined
>;
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment