Now that we've got a basic understanding of what a couple is.
Let's try writing on a little bit of code to understand how we work with them in typescript.
So back and said my editor I'm gonna make another new file inside my project directory called topples
dot T.S. INSIDE OF YOU'RE TO GET STARTED.
I'M GONNA WRITE OUT AN OBJECT THAT LOOKS LIKE THAT DRINK object that we just put together or was inside
that diagram.
So I'll say cons drink is going to be an object with a color of brown carbonated is true and sugar is
up 40 like so.
So a object works perfectly here right.
You and I can read this and very easily understand what's going on.
However we have access to topples as well so we could alternatively try to represent this with a couple
to do so we could write out let's say some new drink here like maybe Pepsi and I'm going to write out
what looks like an array and I'm basically just going to take the different values out of this object
and put them into this array in a very specific order so I'll put in Brown to represent the color then
true to say that it's carbonated and then 48 to represent these sugar content.
In this case like 40 grams say here's the only issue what we just defined right here is an array.
So if you hover over Pepsi right here take a look at that type annotation.
This is saying that Pepsi is going to be a string or a number or a boolean array.
So the issue right now is that we could freely swap the order of elements inside of here.
And in general we don't want to do that because remember the order inside of a table has a very specific
meaning to us.
You and I as developers have to memorize that the first element inside of here represents the color
and the second element represents carbonation.
And the third represents sugar content.
So if you and I start to write out some code that's going to swap the order like let's say Pepsi at
0 is now going to be 40 and Pepsi at 2 is now going to be brown.
We have lost information.
So you and I as developers have an assumption about the order of elements inside of here.
And if we start to break that order our data model completely breaks down so we're not going to define
this thing as a simple looking array and typescript instead we're going to add on an annotation to this
thing this annotation is what's going to turn this line or this array right here into a couple.
So right after Pepsi I'm going to put in a colon and then rather than putting out a type right here
and then square brackets I'm going to instead put in a pair of square brackets and then inside of here
we're going to write out the specific order of types of elements inside of this temple I want right
write string then boolean then no.
Like so.
So this is what makes this thing into a tunnel as opposed to an array.
We are saying that this array is going to always have a very consistent ordering of elements inside
of it always a string then a billion then a number.
So you and I as developers can read this thing right here and understand that there is some meaning
to the order of elements inside of this array looking thing I should say trouble if we now try to change
the order of elements inside of here and say Say Pepsi at 0 is now going to be 40 will very quickly
get an error message.
The error message is going to tell us that we're trying to assign a number to an index inside that tunnel
that is supposed to be a string.
So again by putting on this different looking type annotation right here we are turning this array into
a couple an alternate way of writing this thing out right here rather than putting the type directly
in line as an annotation would be to create something called a Type alias.
We're going to discuss type aliases at great length later on inside this course.
But this is a very good place to use a type alias.
So we're just going to cover them very quickly right now as a quick aside so rather than having to repeat
out this definition right here every time we want to create a drink we can instead right above right
out type.
And then maybe drink with a capital D and then we'll list out the temple structure.
So it's going to be a string boolean and then no this does not create an array right here.
So no array is being created.
Instead it's creating the idea of a topple inside of application.
It's a brand new type that we can freely use in any place that we would normally put a type.
For example in a type annotation.
So now rather than manually writing out all the different types right here I could instead say drink
like so and this is a hundred percent equivalent to what we just had a moment before.
So now I can very easily reuse this temple structure and say maybe sprite is also a type of drink and
it will be clear it is carbonated and maybe it also has a lot of sugar in it maybe we also have tea
that is a drink and maybe it's also brown not carbonated and zero sugar like so so we've now abstracted
out the idea of what a drink is into a type alias.
Now we can freely reuse it throughout our application to indicate that each of these different things
are not plain arrays.
Instead they are a couple that represents a very specific record inside of application.
All right.
Now I'm gonna be honest with you we are not going to use topples very often.
So let's take a quick pause right here.
We're gonna come back the next video we're gonna discuss why we care about the things and where we're
going to use them.
And we're going to find out that maybe temples are not super useful in typescript.
It's a quick pause and I'll see you in just a minute.