Making union out of tuple

Example of TupleToUnion use

Today we discuss Tuple to Union

We already solved a similar challenge Making object out of tuple.

This one is useful when we need to extract elements type out of tuple.

Iteration over a tuple

As we discussed it in Making object out of tuple and Includes in TypeScript, we can use Indexed Access Types to get the type of tuple’s elements:

Solution

Here we use extends any[] as Generic Constrain to be able to call T[number] without errors

All together in Playground with test cases

Have a wonderful day ☀️

typescriptmedium

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 ↘️