beraliv

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

Comments

Alexey Berezin profile image

Written by Alexey Berezin who loves London 🏴󠁧󠁢󠁥󠁮󠁧󠁿, players ⏯ and TypeScript 🦺 Follow me on Twitter