3060 · Unshift
Less than 1 minuteTC-Easy
3060 · Unshift
Problem
Implement the type version of Array.unshift.
type Result = Unshift<[1, 2], 0> // [0, 1, 2]Solution
type Unshift<T extends any[], U> = [U, ...T]Explanation
We use tuple spreading to construct a new tuple that prepends U before all elements of T.
Step by step:
T extends any[]— constrainsTto be an array/tuple type[U, ...T]— placesUfirst, then spreads all elements ofT
This is the mirror of Push: instead of appending to the end, we prepend to the beginning — matching what Array.prototype.unshift does at runtime.
Key concept:
- Variadic Tuple Types —
[U, ...T]creates a new tuple type withUprepended
