Home > other >  Union to Intersection
Union to Intersection

Time:10-10

While skimming through the TypeScript challenges, I came across a particularly interesting one: How to turn a union to an intersection.

Unable to figure it out myself, I turned to the solutions where I found a great approach here and an even greater explanation given here by @jcalz.

The only problem in my way was this question: effectively a user tried to break down the solution in multiple separate statements and, to my surprise, the result was not the same. Instead of foo & bar we were getting foo | bar. Putting the solution back together as a "one-liner" the result gets "restored": foo & bar.

// Type definition
type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) 
  ? I 
  : never;

// As expected            
  • Related