In the last video, we saw that we could remove all these different type annotations and we did not
see any errors, in fact, if we then hovered over these variables.
It's clear that TypeScript still understands what type of value we're going to assign to each given
variable.
So what you're seeing here is the type inference system in action.
In this video, we're going to discuss how type inference works and then discuss when we're going to
rely on type inference versus adding in an annotation manually.
So let's get to it.
All right.
So first off, the basics of type inference.
Anytime you and I make a new variable inside of our application, we're actually undergoing two separate
steps.
So on the left hand side of this equals sign where we say const color, we're doing something called
variable declaration.
This is where we are telling our program that we're going to have a new variable inside of our application
with the given name.
And then in the second step of this entire process, on the right hand side, the equals sign, we are
attempting to assign a value to that variable.
Assigning a value to a variable for the very first time is referred to as variable initialization.
So this entire line of code right here, two separate steps, first variable declaration and then initialization.
Here's the rule around type inference, if we do our declaration and initialization on the same line
or essentially in one single expression, then TypeScript is going to figure out the type of value that
we're going to assign to color for us.
So in this case, TypeScript would look at the variable that were the value that we're usually using
for initialization.
It would see that this is a string and it would say, OK, color must be a variable that's going to
refer to a value of type string.
And that's how we get this kind of redone type right here where we can delete the annotation.
And TypeScript still sees what's going on for us.
If I moved this assignment or this initialization onto a separate line, then it's not going to work
anymore.
So if I say Apple's equals five on the next line down by hover over Apple's, you'll notice that it
has a different annotation on here.
It says any in this case, we're going to discuss with any means in just a moment.
But right now, I think you can agree with me that TypeScript can no longer figure out what type of
value we're going to assign to apples.
So, again, the rule of thumb here is if you're doing the initialization and declaration on the same
line, TypeScript will use type inference for us.
OK, so that's the kind of easy part of type in France would do everything on one line, everything's
going to work just fine.
So now here's the really obvious question.
When are we going to add in these type annotations?
When are we going to type them out?
If we're saying that TypeScript can figure out these different types for us, why would we go through
the process of writing out the invitations manually?
And that's a great question.
Let's take a look at a diagram to understand why we might want to use type inference over annotation
or the opposite.
OK, so in that file we just put together, we've got a lot of examples of type annotations.
So remember, that's where you and I, the developers are going to tell TypeScript the type that a value
or a variable is going to refer to.
Now, type inference, that's where TypeScript is going to guess everything for us.
So in general, we're going to rely on type inference Ollis all the time, wherever we can.
We will rely on type inference.
That means that whenever you and I have a variable that we are declaring and then initializing on the
same line, you and I in this course are not going to add in type annotations.
So we're going to have a lot of code that looks like this right here.
And we will have much more rarely have code that looks like this down here or anything else that you
see inside this file.
So we are going to rely upon type inference as much as we possibly can.
But there are three scenarios where we will rely on type annotations.
So let's take a quick break right here.
And we're going to go through these three different examples of where we are going to add in these type
annotations to help type script out.
So quick pause and I'll see you in just a minute.