From a9a4b4d84970df5d102b18ddf97ffd73a46a7040 Mon Sep 17 00:00:00 2001 From: Josh Rosenstein <32781407+JoshRosenstein@users.noreply.github.com> Date: Sun, 14 Apr 2019 21:17:58 -0400 Subject: [PATCH] feat: add UnionToIntersection --- src/types/utils.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/types/utils.ts b/src/types/utils.ts index b4e9244..a7b6db6 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -34,3 +34,13 @@ export type Nominal = T & Tagged; * @returns a the type union with a promise containing the given type */ export type PromiseOr = Promise | T; + + +/** + * Defines an intersection type of all union items. + * @param U Union of any types that will be intersected. + * @returns U items intersected + * @see https://stackoverflow.com/a/50375286/9259330 + */ +export type UnionToIntersection = + (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;