Yuzhe's Blog

yuzhes

Greater Than

Greater Than

Problem Link

Problem

In this challenge, you should implement a type GreaterThan<T, U> like T > U.

Negative number is not considered.

GreaterThan<2, 1>   // true
GreaterThan<1, 1>   // false
GreaterThan<10, 100> // false
GreaterThan<111, 11> // true

Solution

Approach: Race to Fill a Tuple

Build a tuple concurrently for both numbers. The one whose count reaches the target first is smaller — meaning the other is greater.

type GreaterThan<
  T extends number,
  U extends number,
  Count extends unknown[] = []
> = T extends U
  ? false
  : Count['length'] extends T
    ? false
    : Count['length'] extends U
      ? true
      : GreaterThan<T, U, [...Count, unknown]>

How it works:

  1. If T === U, return false immediately.
  2. We increment Count one step at a time.
  3. If Count['length'] reaches T first, then T ≤ Count before U, so T < Ufalse.
  4. If Count['length'] reaches U first, then U < Ttrue.

Key Takeaways