#27133Medium

Square

Given a number, your type should return its square. Learn tuple manipulation, array type operations in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll implement a Square type that computes the square of a given number at the type level, including support for negative numbers.

Challenge Instructions: Square

Medium

Given a number, your type should return its square.

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

Loading...

Detailed Explanation

type Abs<N extends number> = `${N}` extends `-${infer P extends number}` ? P : N
 
type MakeTuple<N extends number, T extends unknown[] = []> =
  T['length'] extends N ? T : MakeTuple<N, [...T, unknown]>
 
type Multiply<A extends number, B extends number, Result extends unknown[] = [], Count extends unknown[] = []> =
  Count['length'] extends A
    ? Result['length']
    : Multiply<A, B, [...Result, ...MakeTuple<B>], [...Count, unknown]>
 
type Square<N extends number> = Multiply<Abs<N>, Abs<N>>

How it works:

This challenge helps you understand type-level arithmetic using tuple length counting and how to apply these concepts 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