#27152Medium

Triangular number

Given a number N, find the Nth triangular number, i.e. `1 + 2 + 3 + ... + N` Learn tuple manipulation, array type operations in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Triangular type that computes the Nth triangular number (the sum 1 + 2 + 3 + ... + N) entirely at the type level using tuple length arithmetic.

Challenge Instructions: Triangular number

Medium

Given a number N, find the Nth triangular number, i.e. 1 + 2 + 3 + ... + N

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

Loading...

Detailed Explanation

type Triangular<
  N extends number,
  Counter extends any[] = [],
  Acc extends any[] = []
> = Counter['length'] extends N
  ? Acc['length']
  : Triangular<N, [...Counter, 0], [...Acc, ...Counter, 0]>;

How it works:

This challenge helps you understand type-level arithmetic through tuple length manipulation and how to apply recursive accumulation patterns 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