- Now the next thing I wanna talk about
is going to be kind of similar to Enums
and as const,
but this is the idea of tupels.
And tupels is just taking an array of values
but saying that this array of values
is going to be specifically the first value is one type,
the second value is another type,
third value is another type and so on.
So instead of just saying, "I have an array of numbers"
I say, "I have an array where the first index is a number
the second is a string,
the third is a number
and so on and so on."
The perfect example of this being used
in actual JavaScript code
is the object.entries method.
This method right here actually returns to you a tuple.
And a tuple is just saying that it's returning an array
because when you call object entries,
it gives you an array.
And this array is actually an array of arrays
where each individual element array
has the first value being the key of the object,
so it's going to be a string,
and the second value is the value of that key.
So in our case, it'll be either a string or a number.
And if we hover over our key,
you can see it has a type here of string
and we hover over our value,
you can see it has a type of string or number.
That's just because we have strings
or numbers as our values.
Now, the way we would define this ourselves
is relatively straightforward.
So let's just come in here.
I'm gonna create a type here.
And this type is just gonna be called tuple.
And we don't even need this to be an object.
Instead, I'm gonna use an array here
and what whatever I wanna put in this array
is going to be the first value.
So let's say the very first value,
I want to have a string.
And the second value in my array,
I want to be a Boolean.
This is a tuple with two values.
The first is a string, the second is a Boolean.
So now if I create a variable
and I set it to that tuple type,
what I need to make sure I do
is that the first value of my array
must be a string
and the second value must be a Boolean.
And I can't have any other values on top of that.
If I add more values,
you can see it gives me an error,
'cause this expects there
to be only two elements not three.
Or if I, for example, change this to a number
I'm now going to get an error as well.
Right here, this is a number.
It should be a Boolean.
So this is where tupels are really easy to define,
'cause you essentially just create
a hard coded array of different types.
And then when you define your tuple,
you just need to make sure you create the array
and follow the actual type definition.
This is useful in a lot of different places.
If you've ever used react before
the use state hook, for example,
returns a tuple where the first value is your state
and the second value is the updater
for your state function.
Also, object entries is another great example
of our tuple is being returned.
And it may seem like a fairly niche thing,
but there's a lot of different places
where you need to either return an array
or use an array in a specific way
where you know the value of each key.
And this is a perfect use case for a tuple.