Transform tuple type into a union in TypeScript

Example of TupleToUnion use
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, we can use Indexed Access Types to get the type of tuple's elements:

Solution
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 ☀️

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