mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2024-11-22 00:11:02 -05:00
Made a type that converts unions to intersections
This commit is contained in:
parent
2ab31e0c32
commit
09d0d25fdc
1 changed files with 16 additions and 0 deletions
16
src/utils/types/union-to-intersection.ts
Normal file
16
src/utils/types/union-to-intersection.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* Converts a union type to an intersection type.
|
||||
*
|
||||
* This type takes advantage of TypeScript's inference and distributive conditional types.
|
||||
* It starts by using a mapped type to distribute the conditional type over the union type `T`.
|
||||
* For each distributed type, it creates a function type with the type parameter as its argument.
|
||||
* Then, it infers the intersection type `U` by analyzing the compatibility of the function types.
|
||||
* The process results in an intersection type representing the combination of all distributed types.
|
||||
*
|
||||
* @template T - The union type to be converted into an intersection type.
|
||||
*
|
||||
* @returns The intersection type created from the union type `T`.
|
||||
*/
|
||||
export type UnionToIntersection<T> = (
|
||||
T extends unknown ? (x: T) => void : never
|
||||
) extends (x: infer U) => void ? U : never;
|
Loading…
Reference in a new issue