Greater Than
Less than 1 minuteTC-Medium
Greater Than
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> // trueSolution
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:
- If
T === U, returnfalseimmediately. - We increment
Countone step at a time. - If
Count['length']reachesTfirst, thenT ≤ CountbeforeU, soT < U→false. - If
Count['length']reachesUfirst, thenU < T→true.
Key Takeaways
- "Race" logic (which number do we hit first?) is a clean pattern for type-level comparison.
- This approach has O(max(T, U)) recursion depth — works fine for typical challenge inputs.
- For very large numbers a string-based digit comparison would be needed.
