#3060Easy

Unshift

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.

Challenge Instructions: Unshift

Easy

Implement the type version of Array.unshift

For example:

[object Object]

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

ChallengeSolution
/* _____________ Your Code Here _____________ */

type Unshift<T, U> = any

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '../helpers'

type cases = [
  Expect<Equal<Unshift<[], 1>, [1]>>,
  Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
  Expect<Equal<Unshift<['1', 2, '3'], boolean>, [boolean, '1', 2, '3']>>,
]

Pro Challenge

Unlock 102+ medium, hard, and extreme challenges to master advanced TypeScript.

One-time payment. Lifetime access.

Video Walkthrough

Detailed Explanation

The Unshift type uses the spread operator to prepend an element to a tuple.

[object Object]

How it works:

This 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.

Share this challenge