#3057Easy

Push

Add elements to the end of tuples with TypeScript's type system. Master the spread operator and tuple manipulation in this easy-level challenge on TypeScriptPro.

Add elements to arrays at the type level with Push! ➕

In this easy-level challenge, you'll implement the generic version of Array.push in TypeScript's type system. This type takes a tuple and an element, returning a new tuple with the element appended to the end.

This challenge reinforces your understanding of the spread operator in tuple types and shows how TypeScript can model array operations at compile time. It's a fundamental building block for more complex array manipulations.

For this challenge, you will need to change the following code to make the tests pass (no type check errors).

Challenge Instructions: Push

Easy

Implement the generic version of Array.push

For example:

type Result = Push<[1, 2], '3'> // [1, 2, '3']
Loading...

Video Walkthrough

Detailed Explanation

The Push type uses the spread operator to append an element to a tuple.

type Push<T extends unknown[], U> = [...T, U]

How it works:

This elegantly models JavaScript's push operation at the type level, maintaining type safety for each element in the resulting tuple. The simplicity of this solution showcases TypeScript's expressive power.

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

This challenge is originally from here.