Implement array concatenation at the type level with TypeScript. Learn about tuple types and the spread operator in this easy-level challenge on TypeScriptPro.
Join arrays together at the type level with Concat! 🔗
In this easy-level challenge, you'll implement the JavaScript Array.concat
function in TypeScript's type system. This type takes two tuple arguments and outputs a new tuple that includes all elements from both inputs in left-to-right order.
You'll learn about TypeScript's spread operator in tuple types, a powerful feature for manipulating arrays at the type level. This challenge is perfect for understanding how to work with variadic tuple types.
For this challenge, you will need to change the following code to make the tests pass (no type check errors).
Implement the JavaScript Array.concat
function in the type system. A type takes the two arguments. The output should be a new array that includes inputs in ltr order
For example:
type Result = Concat<[1], [2]> // expected to be [1, 2]
The Concat type uses TypeScript's spread operator to merge tuples at the type level.
type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [
...T,
...U,
]
How it works:
T extends readonly unknown[]
and U extends readonly unknown[]
ensure both inputs are array-like[...T, ...U]
spreads both tuples into a new tupleThis mirrors JavaScript's array spread syntax but operates entirely at the type level, demonstrating the elegance of TypeScript's type system in modeling runtime behaviors.
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.