Now we've seen a couple of examples of arrays.
I want to walk through some kind of corner cases around them.
So essentially I want to discuss why we care about these rates like what's so big a deal about arrays
and typescript that we're making this into a separate topic.
Well here's why.
When we work with the raisin typescript we got a couple of different advantages in a couple of different
downsides as well.
So first off typescript can help us when we make a typed array by helping us do type inference whenever
we are pulling a value out of an array.
Let's see a quick example of that.
So back over inside my code editor I'm gonna put a little comma down right here and I'll say help with
inference when extracting values.
So in other words if we look at our carmaker's array right here we know that it is an array of strings
typescript knows that as well through type inference.
So that means that if we ever pull an element out of this array either by using say the pop method or
by direct index access typescript will know that we are going to be pulling out a string and help us
with type inference with the new variable that we're assigning this this value to.
So let's take a look at an example of that.
Let's say that I'm making a new variable right here and I'm going to call it something like car if I
then take car makers and do an index access to it.
I can now hover over a car and you'll see that type inference has come into play and typescript knows
that this variable is going to be a string.
It knows this because it knows that car makers is an array of strings and we just pulled one value out
of it likewise.
This also works if we try a pop method.
So let's say we've got my car.
Would you like car makers not pop like so to remove the last element in the array.
Once again typescript knows that carmakers has an array of strings.
So when we call pop it's going to return an instance of a string so we can once again hover over my
car and we see yet typescript has correctly inferred that my car is going to be a string all right.
So that's one benefit of making use of arrays and typescript.
Secondly typescript can help us prevent from or help prevent us from adding in incompatible values into
an array.
If we have correctly type the array.
So for example back over here once again we can say prevent in compatible values.
So once again if we take our car makers array right here that only contains strings and we tried to
add in some element that is not a string like let's say a number will very quickly see an error message
typescript is telling us here hey you're trying to put in a number to a collection that is only supposed
to contain strings.
So that's going to help us from putting in dissimilar elements into one single array and once again
if we want to have multiple different types in an array we definitely can do that.
But we have to use some special syntax to do so.
We'll take a look at an example of that in just a moment.
All right.
Third anytime we declare an array we get a lot of help with built in functions like map for each reduce.
And so on whenever we are iterating over a collection of elements.
So for example if we look back over once again we get help with map or for each and reduce.
So for example if we do car makers map we can add in a function right here that will be called with
every element in the array.
So I'm going to receive each car as a string and from this I'm going to return say a string as well
we'll just do a straight pass through here.
So I'll take the car and return it immediately so typescript is going to make sure that these value
that we are putting into this function right here will be a string and so because of that because typescript
knows that car is going to be a string we get access to a lot of different autocomplete on the variable
that comes in here.
So for example if I put in a dot right after a car I can then see the auto complete appear with all
the different methods that belong to strings.
So for example I can do a two upper case like so so anytime we start working with map for each reduce
or any similar helpers typescript is going to give us auto complete on the variable being passed into
this function right here which is a nice little benefit all right.
Now the last item we're going to talk about right here is that we can in fact put different types of
elements inside of an array let's take a quick pause though because there's a couple of different corner
cases around this and some interesting syntax we need to understand at the same time.
It's a quick pause and I'll see you in just a minute.