Now that we've seen a couple of examples of raise, 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 a raise in typescript that we're making this into a separate topic?
Well, here's why.
When we work with the raise, typescript, we got a couple of different advantages and a couple of different
downsides as well.
So first off, TypeScript can help us when we make a type turei by helping us do type inference whenever
we are pulling a value out of an array.
Let's see a quick example of that.
Zoback back over inside my code editor.
I'm going to put a little comment 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 car and you'll see that
type inference has come into play in.
TypeScript knows that this variable is going to be a string.
It knows this because it knows that carmakers 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 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 up.
TypeScript has correctly inferred that my car is going to be a string.
All right.
That's one benefit of making use of a raise and typescript secondly, typescript can help us prevent
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 compatable.
Values.
So once again, if we take our carmakers away 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, any time 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 rakia 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 the value that we are putting into this function right here
will be a string.
And so because of that, because TypeScript knows that Corey 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 car, I can then see the autocomplete appear with all
the different methods that belong to strings.
So, for example, I can do a two upper.
Case like so.
So any time we start working with bat for each reduce or any similar, Helper's typescript is going
to give us autocomplete 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.