TypeScript spread operator for 2 tuple types

Example of Concat use
Example of Concat use

Ninth challenge is Concat

Sometimes it's useful to collect 2 tuples' elements in one tuple, like Array.prototype.concat.

Using spread for Tuple types

2 changes were introduced in TypeScript 4.0:

  1. Variadic Tuple Types
  2. You can use spread anywhere in a tuple, not just an end

So let's try it all together:

Concat solution, v1
Concat solution, v1

Here we have an error A rest element type must be an array type, let's add Generic Constrains to allow only arrays:

Concat final solution
Concat final solution

This is it 🚀

Don't forget to have a look at Playground

typescript

Alexey Berezin

Senior Software Engineer, UK Global Talent 🇬🇧

The portrait photo of me

Thank you for reading my blog post! If you have any questions, suggestions, or just want to say hi, feel free to reach out in social networks, or via email. I would love to hear from you!

Comments