beraliv

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 profile image

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