Yuzhe's Blog

yuzhes

Construct Tuple

Construct Tuple

Problem Link

Problem

Construct a tuple with a given length.

type result = ConstructTuple<2>  // [unknown, unknown]

Solution

Approach: Accumulator until Target Length

Recursively grow a tuple until its length equals L.

type ConstructTuple<
  L extends number,
  T extends unknown[] = []
> = T['length'] extends L
  ? T
  : ConstructTuple<L, [...T, unknown]>

How it works:

  1. Start with an empty tuple T = [].
  2. If T['length'] === L, return T.
  3. Otherwise, append one unknown and recurse.

Key Takeaways