1
00:00:01,080 --> 00:00:05,280
In this video, we're going to take a look at another example of when we're going to add in a type annotation

2
00:00:05,280 --> 00:00:05,780
manually.

3
00:00:06,150 --> 00:00:11,100
So this is going to be whenever we declare a variable on one line and then initialize it later as a

4
00:00:11,100 --> 00:00:15,840
quick reminder type inference only works when we do a variable declaration and initialization on the

5
00:00:15,840 --> 00:00:16,360
same line.

6
00:00:16,650 --> 00:00:21,180
And so, obviously, if we split this into two separate steps or two different lines of code, then

7
00:00:21,180 --> 00:00:22,700
type inference doesn't work anymore.

8
00:00:23,600 --> 00:00:27,680
All right, so I'm going to put back over to my editor again at the very bottom of my variables file.

9
00:00:28,010 --> 00:00:29,490
I'm going to set up a second example here.

10
00:00:29,490 --> 00:00:36,680
I'm going to say when we declare a variable on one line and initialize it later.

11
00:00:37,760 --> 00:00:43,460
OK, so for this example, I'm going to make an array of strings called words like so and I'm going

12
00:00:43,460 --> 00:00:45,110
to give this the words red.

13
00:00:46,780 --> 00:00:48,670
Green and blue.

14
00:00:49,570 --> 00:00:54,160
Then right after that, I'm going to declare a variable called bound word like so.

15
00:00:55,050 --> 00:00:59,370
So here's what I want to do, I want to write a for loop that's going to iterate through this array

16
00:00:59,370 --> 00:00:59,910
right here.

17
00:01:00,720 --> 00:01:07,050
And if I ever find a word or a string equal to green, then I want to set found word to true.

18
00:01:08,280 --> 00:01:12,680
So if you find green inside of words just set downward to true, that's pretty much it.

19
00:01:12,990 --> 00:01:17,520
I know this is a silly example, but I just want to give you a very practical example of when we're

20
00:01:17,520 --> 00:01:21,420
going to run into this type annotation stuff that's going to write out my for loop.

21
00:01:21,600 --> 00:01:28,380
I'm going to say let equals zero I less than words length I plus plus.

22
00:01:29,260 --> 00:01:37,720
And then for every word will say, if words at eye is equal to green and we'll set found word equal

23
00:01:37,930 --> 00:01:39,340
to true like so.

24
00:01:40,980 --> 00:01:46,110
It's now in this case, I have an instance where I'm declaring the variable up here and then I only

25
00:01:46,110 --> 00:01:50,580
assign it a value or initialize it down here inside of my for for a loop.

26
00:01:51,450 --> 00:01:55,710
If I know mouseover found word and will very clearly tell me that this is of type any and in fact I

27
00:01:55,710 --> 00:02:00,670
even have a little underline here, that is a warning that tells me that found word has an implicit

28
00:02:00,690 --> 00:02:01,410
any type.

29
00:02:02,100 --> 00:02:06,420
And then the tip here says that I might get a better type if I actually use the variable.

30
00:02:07,690 --> 00:02:13,030
Now, when it says inferred from usage, that actually means that, like I have to add in the annotation

31
00:02:13,030 --> 00:02:15,460
right here or optionally initialize it on the same line.

32
00:02:16,150 --> 00:02:21,250
So in this case, to make this morning go away, we have to add in a type annotation so I can tell,

33
00:02:21,250 --> 00:02:21,820
TypeScript.

34
00:02:22,090 --> 00:02:23,410
All right, there's going to be a boolean.

35
00:02:24,310 --> 00:02:30,250
And now we get around that issue, so it never type inference doesn't work because we are doing declaration

36
00:02:30,250 --> 00:02:32,100
and initialization on separate lines.

37
00:02:32,140 --> 00:02:33,850
We're going to add any type annotation.

38
00:02:33,950 --> 00:02:34,750
That's pretty much it.

39
00:02:35,590 --> 00:02:39,970
Now, as a quick aside here, obviously the best way to write out this code would have been to do something

40
00:02:39,970 --> 00:02:42,010
like this, to initialize found where it is false.

41
00:02:42,250 --> 00:02:44,510
And now TypeScript understands what's going on here.

42
00:02:44,800 --> 00:02:47,200
This is way better code than what we had before.

43
00:02:47,320 --> 00:02:51,880
But again, I just wanted to give you a quick example of where we might have a declaration on one line

44
00:02:51,880 --> 00:02:54,100
and then initialization at some future point.

45
00:02:55,190 --> 00:02:56,720
All right, so let's take a quick pause right here.

46
00:02:56,720 --> 00:03:00,440
When we come back in the next video, we're going to take a look at our final example of when we are

47
00:03:00,440 --> 00:03:02,540
going to prefer to use a type invitation.

48
00:03:02,840 --> 00:03:04,610
It's a quick pause and I'll see you in just a minute.

