Infer first element of tuple type in TypeScript

Example of First use
Example of First use

Fourth challenge is First of Array

We can use it for both arrays and tuples, but for tuples you get exact value.

To distinguish arrays and tuples, please visit Making object out of tuple

Extracting first element

Previously we described how to iterate over tuples. For this challenge we don't need it as we can access index directly using [0]:

Solution
Solution

But don't forget about Generic Constrains using extends (it's already added). Without it we cannot call T[0] and will get an error Type '0' cannot be used to index type 'T'

Thank you for your time! 🕒

If you interested in test cases, please 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