How to merge Arrays in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
3 min

banner

# Merge Arrays in TypeScript

Use the spread syntax to merge arrays in TypeScript. The spread syntax will unpack the values of the arrays into a new array.

The new array will have a type that reflects the types of the values in the supplied arrays.

index.ts
const arr1 = ['bobby', 'hadz']; const arr2 = ['.', 'com']; // ๐Ÿ‘‡๏ธ const arr3: string[] const arr3 = [...arr1, ...arr2]; console.log(arr3); // ๐Ÿ‘‰๏ธ [ 'bobby', 'hadz', '.', 'com' ]

merge arrays in typescript

The code for this article is available on GitHub

We used the spread syntax (...) to unpack the elements of 2 arrays into a third array.

The type of the final array will reflect the type of the provided arrays.

index.ts
const arr1 = ['bobby', 'hadz']; const arr2 = [1, 2]; // ๐Ÿ‘‡๏ธ const arr3: (string | number)[] const arr3 = [...arr1, ...arr2]; // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 1, 2 ] console.log(arr3);

We used the spread syntax (...) to unpack string and numeric arrays into a third array.

The type of the third array is a union representing an array of strings and numbers.

Here is an example that explicitly types the arrays.

index.ts
const arr1: string[] = ['bobby', 'hadz']; const arr2: number[] = [1, 2]; // ๐Ÿ‘‡๏ธ const arr3: (string | number)[] const arr3: (string | number)[] = [...arr1, ...arr2]; // ๐Ÿ‘‡๏ธ [ 'bobby', 'hadz', 1, 2 ] console.log(arr3);

explicitly typing the arrays

If you try to add values of any other type than the specified type to the array, the type checker throws an error.

The syntax (string | number)[] is called a union and means that the array can contain strings and numbers.

Note that the syntax for typing an array in TypeScript is type[] and not [type].

The [type] syntax is used to declare a tuple containing a single element of type type.

The type[] syntax is used to declare an array that contains zero or more elements of type type.

When using the spread syntax, the order in which you unpack the arrays is important.

index.ts
const arr1 = ['a', 'b']; const arr2 = ['c', 'd']; // ๐Ÿ‘‡๏ธ const arr3: string[] const arr3 = [...arr2, ...arr1]; // ๐Ÿ‘‡๏ธ ['c', 'd', 'a', 'b'] console.log(arr3);

This process can be repeated with as many arrays as necessary.

index.ts
const arr1 = ['a', 'b']; const arr2 = ['c', 'd']; const arr3 = ['e', 'f']; // ๐Ÿ‘‡๏ธ const arr3: string[] const arr4 = [...arr1, ...arr2, ...arr3]; // ๐Ÿ‘‡๏ธ ['a', 'b', 'c,' 'd', 'e', 'f'] console.log(arr4);

I've also written a detailed article on how to create a union type from an array or object.

# Merge Arrays in TypeScript using concat()

You might also see the Array.concat() method used to merge arrays in TypeScript.

index.ts
const arr1 = ['a', 'b']; const arr2 = ['c', 'd']; const arr3 = ['e', 'f']; // ๐Ÿ‘‡๏ธ const arr3: string[] const arr4 = arr1.concat(arr2, arr3); // ๐Ÿ‘‡๏ธ ['a', 'b', 'c,' 'd', 'e', 'f'] console.log(arr4);

merge arrays in typescript using concat

The code for this article is available on GitHub

The concat method also merges two or more arrays. The method takes one or more arrays as parameters and merges them into the array on which it was called.

However, you should stick to using the spread syntax (...), because the typings for the concat method in TypeScript are not as intuitive.

index.ts
const arr1 = ['a', 'b']; const arr2 = [1, 2]; // โ›”๏ธ Type 'number[]' is not assignable to // type 'ConcatArray<string>'.ts(2769) const arr3 = arr1.concat(arr2);

The code sample causes the type checker to throw an error because we called the concat() method on an array of strings and passed it a numeric array.

The typings expect you to only pass arrays of the same type as parameters to the concat() method.

This is not the case when working with the spread syntax (...) which is much more flexible.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev