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).
Implement the generic version of Array.push
For example:
type Result = Push<[1, 2], '3'> // [1, 2, '3']
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:
T extends unknown[]
ensures T is an array type[...T, U]
spreads all elements of T, then adds U at the endThis 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.
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
This challenge is originally from here.