TypeScript Snippet

UnionToIntersection Utility Type

Difficulty: Hard

Going from a union (`A | B | C`) to an intersection (`A & B & C`) is what you need when you want to merge handler shapes, deduce the most-specific type, or express a 'must satisfy all branches' constraint. TypeScript has no built-in for this, but a single conditional with `infer` does the job. This snippet builds the type, walks through how distributive conditionals make it work, and ties it to a concrete LastInUnion / merged-handlers example.

Code Snippets
/

UnionToIntersection Utility Type

UnionToIntersection Utility Type

Going from a union (`A | B | C`) to an intersection (`A & B & C`) is what you need when you want to merge handler shapes, deduce the most-specific type, or express a 'must satisfy all branches' constraint. TypeScript has no built-in for this, but a single conditional with `infer` does the job. This snippet builds the type, walks through how distributive conditionals make it work, and ties it to a concrete LastInUnion / merged-handlers example.

TypeScript
Hard
ts-utility-types
ts-conditional-types
ts-infer-keyword
ts-generics

194 views

5

This code snippet is available for premium members only.

Upgrade to Premium