Square
Less than 1 minuteTC-Medium
Square
Problem
Given a number N, return its square.
type T0 = Square<0> // 0
type T1 = Square<1> // 1
type T2 = Square<3> // 9
type T3 = Square<-3> // 9 (square of absolute value)Solution
// Build a tuple of length N
type BuildTuple<N extends number, T extends unknown[] = []> =
T['length'] extends N ? T : BuildTuple<N, [...T, unknown]>
// Absolute value for number type (handles negatives via string)
type Abs<N extends number> =
`${N}` extends `-${infer P extends number}` ? P : N
// Concatenate two tuples and return length = A + B
type Add<A extends number, B extends number> =
[...BuildTuple<A>, ...BuildTuple<B>]['length']
// Repeat addition B times: A * B
type Multiply<
A extends number,
B extends number,
Count extends unknown[] = [],
Acc extends unknown[] = []
> =
Count['length'] extends B
? Acc['length']
: Multiply<A, B, [...Count, unknown], [...Acc, ...BuildTuple<A>]>
type Square<N extends number> = Multiply<Abs<N>, Abs<N>>How it works:
Abs<N>strips a leading-from the string representation to get the positive value.BuildTuple<N>constructs a tuple of lengthNfor use in arithmetic.Multiply<A, B>accumulatesAcopies of a length-Atuple, iteratingBtimes.Square<N>callsMultiply<Abs<N>, Abs<N>>.
Limitation: Works for small numbers due to TypeScript's recursion depth limit.
Key Takeaways
- Type-level multiplication is repeated addition; addition is tuple concatenation.
- Handling negatives: convert to string, strip
-, parse back withinfer N extends number. - For larger numbers a string-digit approach is needed, but tuple arithmetic is cleaner for small inputs.
