Spread in Tuple types in TypeScript

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

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

This is it 🚀

Don’t forget to have a look at Playground


Let's chat and have fun 🧑‍💻👩‍💻

No more often than once a week I write 📄 about TypeScript 💪, video playback 📺 and frontend related topics. You can unsubscribe in any time ↘️