Add elements to the beginning of tuples with TypeScript. Learn tuple manipulation and spread operators in this easy-level challenge on TypeScriptPro.
Prepend elements to arrays at the type level with Unshift! ⬅️
In this easy-level challenge, you'll implement the type version of Array.unshift in TypeScript. This type takes a tuple and an element, returning a new tuple with the element added at the beginning.
Like its JavaScript counterpart, this type operation demonstrates how to manipulate tuple positions at compile time. You'll solidify your understanding of spread operators and tuple type construction.
Implement the type version of Array.unshift
For example:
type Result = Unshift<[1, 2], 0> // [0, 1, 2]Change the following code to make the test cases pass (no type check errors).
The Unshift type uses the spread operator to prepend an element to a tuple.
type Unshift<T extends unknown[], U> = [U, ...T]How it works:
T extends unknown[] ensures T is an array type[U, ...T] places U first, then spreads all elements of TThis mirrors JavaScript's unshift method perfectly, showing how TypeScript's type system can model array operations with the same intuitive syntax used at runtime.
This challenge is originally from here.
Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.
Only 27 Spots left
Get 1 month early access
Pre-Launch discount