#27862Medium

CartesianProduct

Given 2 sets (unions), return its Cartesian product in a set of tuples, e.g. Learn union type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement CartesianProduct<T, U> which takes two union types and returns a union of all possible tuple pairs combining one member from each set.

Challenge Instructions: CartesianProduct

Medium

Given 2 sets (unions), return its Cartesian product in a set of tuples, e.g.

CartesianProduct<1 | 2, 'a' | 'b'>
// [1, 'a'] | [2, 'a'] | [1, 'b'] | [2, 'b']

Change the following code to make the test cases pass (no type check errors).

Loading...

Detailed Explanation

type CartesianProduct<T, U> = T extends T
  ? U extends U
    ? [T, U]
    : never
  : never

How it works:

This challenge helps you understand distributive conditional types and how to leverage them for generating combinatorial union types in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts

Join early, learn faster.

Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.

No spam, unsubscribe at any time. We respect your privacy.

Limited Availability

Only 27 Spots left

Early Access

Get 1 month early access

>75% Off

Pre-Launch discount