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 a 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 I'm going to put back over to my editor again at the very bottom of my variables file.
I'm going to set up a second example here.
I'm 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 red.
Green and blue.
Then right after that, I'm going to declare a variable called bound word like so.
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 true.
So if you find green inside of words just set downward 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 that's going to write out my for loop.
I'm going to say let equals zero I less than words length I plus plus.
And then for every word will say, if words at eye is equal to green and we'll set found word equal
to true like so.
It's 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 a loop.
If I know mouseover found word and will very clearly tell me that this is of type any and in fact I
even have a little underline 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 morning go away, we have to add in a type annotation so I can tell,
TypeScript.
All right, there's going to be a boolean.
And now we get around that issue, so it never type inference doesn't work because we are doing declaration
and initialization on separate lines.
We're going to add any type annotation.
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 where it 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, so 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.