In this video we're going to look at the final case in which we will make use of type annotations.
So this will be whenever we have a variable where its type can not be reasonably inferred even if the
declaration and initialization are on the same line.
This is really going to be a very big corner case right here.
Nonetheless I just want you to understand when we want to use type annotations so let's try to get through
this example really quickly going to flip back over to my editor and at the very bottom once again I'm
going to add in a new section down here and I'll say this is the third case this is when we have a variable
whose type cannot be inferred correctly for this example.
We're going to create a array of numbers called numbers.
I'm going to give it values like negative 10 negative 1 and twelve 0 then declare a variable called
number above zero.
So here's the idea I want to iterate through the numbers array.
If we find a number that is greater than zero I want to assign it to number above zero.
Otherwise I want to assign false to number above zero.
So that means that we're going to have two different types of values being assigned to this variable.
Now this is some kind of bad code or writing right here.
I really don't recommend taking this approach in general.
However there are scenarios where you might need to do something like this doing it with a number in
a boolean doesn't make a lot of sense but there might be other cases like maybe you're trying to find
a user's favorite piece of media and maybe possible choices for media are like a blog post or an image
or a book or a movie or something like that.
So if we had something like you know user's favorite media it would possibly make sense to assign some
different type of values to this variable.
So this is kind of where this scenario is going to come up.
All right.
So we're going to say that by default number above zero is going to be false.
And then if we ever find something above zero inside this rate we need to assign that number to number
above zero.
So let's write this code out I'll say for what I equal zero I'm less than numbers dot length I plus
plus.
And then if numbers that I is greater than zero will say no above zero is going to be that number.
So that's Give me numbers at I.
As soon as we put that in we're going to see an error message.
And I definitely would expect to see this.
So type inference already kicked in right here on this line of code.
We assigned false to number above zero.
So typescript very correctly thinks that this is supposed to be of type boolean.
Then on this line of code right here we're trying to assign something that is definitely a number to
that variable.
So the area is that we're trying to assign a number to a variable that is supposed to reference a boolean.
So this is one the downsides of type inference type inference kicked in here it did its job but it didn't
really understand the intent of our code or what we're trying to do so because type inference is marking
this variable as always be a being a boolean.
Our code won't work as is.
So to fix this we could add in a type annotation so we could add in something that says Colin and then
boolean or no.
Like so this character right here is the pipe it is none L..
It's not an I is a character right above the return key on your keyboard you can picture this as being
like an overstatement.
So this is saying that no above zero is going to be either a boolean value or it might be a number one
of the two.
So any time we need to express kind of like an or statement with our types we will have to use a type
annotation type inference won't work correctly so by using this type annotation typescript now understands
that we can assign a value of type boolean or a value of type number two is variable and if we mouse
over that variable it'll be reflected right there as well.
All right.
Like I said this is kind of a bad code example but we could kind of easily imagine some other scenarios
where we might have to express a single variable that has possibly different types and we definitely
are going to add some code that looks like this in a future project in this course.
So we'll see this kind of scenario of where we have to add in a type annotation but with they can have
more practical approach.
All right.
So that's pretty much it.
So we now have taken a look at the three different cases where we definitely will have to use a type
annotation.
Let's now take a quick pause.
We're gonna move on in the next video and talk about type annotations and inference around functions
and objects as well where's that diagram right here.
Functions and objects and are going to get through these other approaches really quickly because we
just had to do some like really original work in this variable section so click pause and I'll see you
in just a minute.