#4471Medium

Zip

In This Challenge, You should implement a type `Zip<T, U>`, T and U must be `Tuple` Learn tuple manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Zip type that pairs up corresponding elements from two tuples into a tuple of pairs, stopping at the length of the shorter tuple.

Challenge Instructions: Zip

Medium

In This Challenge, You should implement a type Zip<T, U>, T and U must be Tuple

[object Object]

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

ChallengeSolution
type cases = [
  Expect<Equal<Zip<[], []>, []>>,
  Expect<Equal<Zip<[1, 2], [true, false]>, [[1, true], [2, false]]>>,
  Expect<Equal<Zip<[1, 2, 3], ['1', '2']>, [[1, '1'], [2, '2']]>>,
  Expect<Equal<Zip<[], [1, 2, 3]>, []>>,
  Expect<Equal<Zip<[[1, 2]], [3]>, [[[1, 2], 3]]>>,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

Detailed Explanation

type Zip<T extends any[], U extends any[]> =
  T extends [infer TFirst, ...infer TRest]
    ? U extends [infer UFirst, ...infer URest]
      ? [[TFirst, UFirst], ...Zip<TRest, URest>]
      : []
    : [];

How it works:

This challenge helps you understand parallel tuple traversal and recursive type pairing, and how to apply these concepts in real-world scenarios.

This challenge is originally from here.

Share this challenge