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 it's type cannot be reasonably inferred, even if
the declaration and initialization are on the same line.
This is really going to be a very big court 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.
I'm 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 who is 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 ten, negative one and 12.
I'll 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 and 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, users favorite media, it would possibly make sense to assign
some different type of values to this variable.
So this is kind of where the 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 array, we need to assign that number to number
above zero.
So let's write this code out.
Let's say four let equals zero, i.e. less than numbers that length.
Plus, plus, and then if numbers that eye is greater than zero, we'll say no above zero is going to
be that number.
So let'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 assign 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 error is that we're trying to assign a number to a variable that is supposed to reference a boolean.
So this is one of 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 in France is marking this variable, as always, 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 Kullen.
And then Boolean.
Or no, like so this character right here is the pipe.
It is not an L. It's not an eye is a character right above the return key on your keyboard.
You can picture this as being like an understatement, so this is saying that number above zero is going
to be either a boolean value or it might be a number one of the two.
So any time that we need to express kind of like an overstatement 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 to its variable, and if we mouseover 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 a 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.
So let's now take a quick pause.
We're going to 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 we're 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 quick pause and I'll see you in just a minute.