Infer first element of tuple type in TypeScript
8 Apr 2021
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]
:
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