#34007Medium

Compare Array Length

Implement `CompareArrayLength` to compare two array length(T & U). Learn array type operations in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement CompareArrayLength<T, U> which compares the lengths of two arrays, returning 1 if T is longer, -1 if U is longer, and 0 if they are equal.

Challenge Instructions: Compare Array Length

Medium

Implement CompareArrayLength to compare two array length(T & U).

If length of T array is greater than U, return 1; If length of U array is greater than T, return -1; If length of T array is equal to U, return 0.

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

Loading...

Detailed Explanation

type CompareArrayLength<T extends any[], U extends any[]> =
  T extends [any, ...infer TRest]
    ? U extends [any, ...infer URest]
      ? CompareArrayLength<TRest, URest>
      : 1
    : U extends [any, ...any[]]
      ? -1
      : 0

How it works:

This challenge helps you understand recursive tuple deconstruction for comparing structural properties of arrays and how to apply this concept in real-world scenarios.

This challenge is originally from here.

Share this challenge

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