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 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 okay 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 pre done type right here where we can delete the annotation and typescript
still sees what's going on for us.
If I move this assignment or this initialization onto a separate line then it's not going to work anymore.
So if I say apples equals five on the next line down by hover over apples 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 and 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 for doing the initialization and declaration on the same line
typescript we'll use type inference for us OK.
So that's the easy part of type inference.
We do everything on one line.
Everything is 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
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.
So in that file if 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 all these 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 and type annotations.
So we're gonna have a lot of code that looks like this right here and we will 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 typescript out so quick pause and I'll
see you in just a minute.