In this video we're going to take a look at another example of when we're going to add in a type annotation
manually.
So this is going to be whenever we declare a variable on one line and then initialize it later as a
quick reminder type inference only works when we do variable declaration and initialization on the same
line.
And so obviously if we split this into two separate steps or two different lines of code then type inference
doesn't work anymore all right.
So going to flip back over to my editor I'm again at the very bottom of my variables file I'm going
to set up a second example here going to say when we declare a variable on one line and initialize it
later OK.
So for this example I'm going to make an array of strings called Words like so and I'm going to give
this the words read green and blue then right after that I'm going to declare a variable called Bound
word like s.
So here's what I want to do.
I want to write a for loop that's going to iterate through this array right here.
And if I ever find a word or a string equal to green then I want to set found word to a true so if finds
green inside of words just set found where to true.
That's pretty much it.
I know this is a silly example but I just want to give you a very practical example of when we're going
to run into this type annotation stuff so I got to write out my for loop and say let I equals zero I
less than words at length I plus plus and then for every word will say if words at i is equal to green
then we'll set found word equal to true like so now in this case I have an instance where I'm declaring
the variable up here and then I only assign it a value or initialize it down here inside of my for for
loop if I know mouse over found word it will very clearly tell me that this is of type any and in fact
I even have a little underlined here that is a warning that tells me that found word has an implicit
any type.
And then the tip here says that I might get a better type if I actually use the variable.
Now when it says inferred from usage that actually means that like I have to add in the annotation right
here or optionally initialize it on the same line.
So in this case to make this warning go away we have to add in a type annotation.
So I can tell typescript.
All right.
There's gonna be a boolean and now we get around that issue.
So whenever type inference doesn't work because we are doing declaration and initialization on separate
lines we're going to add any type invitation.
That's pretty much it.
Now as a quick aside here obviously the best way to write out this code would have been to do something
like this to initialize found word is false.
And now typescript understands what's going on here.
This is way better code than what we had before.
But again I just wanted to give you a quick example of where we might have a declaration on one line
and then initialization at some future point.
All right.
Let's take a quick pause right here.
When we come back in the next video we're going to take a look at our final example of when we are going
to prefer to use a type invitation.
It's a quick pause and I'll see you in just a minute.