1 00:00:01,190 --> 00:00:05,300 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,300 --> 00:00:06,200 manually. 3 00:00:06,230 --> 00:00:11,090 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,090 --> 00:00:16,070 quick reminder type inference only works when we do variable declaration and initialization on the same 5 00:00:16,070 --> 00:00:16,340 line. 6 00:00:16,750 --> 00:00:21,740 And so obviously if we split this into two separate steps or two different lines of code then type inference 7 00:00:21,740 --> 00:00:23,940 doesn't work anymore all right. 8 00:00:23,940 --> 00:00:28,200 So going to flip back over to my editor I'm again at the very bottom of my variables file I'm going 9 00:00:28,200 --> 00:00:36,210 to set up a second example here going to say when we declare a variable on one line and initialize it 10 00:00:36,300 --> 00:00:38,080 later OK. 11 00:00:38,090 --> 00:00:43,640 So for this example I'm going to make an array of strings called Words like so and I'm going to give 12 00:00:43,640 --> 00:00:52,980 this the words read green and blue then right after that I'm going to declare a variable called Bound 13 00:00:53,010 --> 00:00:54,940 word like s. 14 00:00:55,110 --> 00:00:56,370 So here's what I want to do. 15 00:00:56,370 --> 00:01:00,810 I want to write a for loop that's going to iterate through this array right here. 16 00:01:00,810 --> 00:01:09,060 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 17 00:01:09,300 --> 00:01:11,850 green inside of words just set found where to true. 18 00:01:11,850 --> 00:01:13,090 That's pretty much it. 19 00:01:13,110 --> 00:01:17,670 I know this is a silly example but I just want to give you a very practical example of when we're going 20 00:01:17,670 --> 00:01:24,500 to run into this type annotation stuff so I got to write out my for loop and say let I equals zero I 21 00:01:24,510 --> 00:01:34,180 less than words at length I plus plus and then for every word will say if words at i is equal to green 22 00:01:35,070 --> 00:01:43,830 then we'll set found word equal to true like so now in this case I have an instance where I'm declaring 23 00:01:43,830 --> 00:01:50,310 the variable up here and then I only assign it a value or initialize it down here inside of my for for 24 00:01:50,310 --> 00:01:55,640 loop if I know mouse over found word it will very clearly tell me that this is of type any and in fact 25 00:01:55,640 --> 00:02:00,650 I even have a little underlined here that is a warning that tells me that found word has an implicit 26 00:02:00,740 --> 00:02:01,830 any type. 27 00:02:02,210 --> 00:02:07,710 And then the tip here says that I might get a better type if I actually use the variable. 28 00:02:07,790 --> 00:02:13,160 Now when it says inferred from usage that actually means that like I have to add in the annotation right 29 00:02:13,160 --> 00:02:16,210 here or optionally initialize it on the same line. 30 00:02:16,220 --> 00:02:20,460 So in this case to make this warning go away we have to add in a type annotation. 31 00:02:20,780 --> 00:02:22,040 So I can tell typescript. 32 00:02:22,190 --> 00:02:22,550 All right. 33 00:02:22,550 --> 00:02:26,640 There's gonna be a boolean and now we get around that issue. 34 00:02:26,940 --> 00:02:31,620 So whenever type inference doesn't work because we are doing declaration and initialization on separate 35 00:02:31,620 --> 00:02:34,020 lines we're going to add any type invitation. 36 00:02:34,020 --> 00:02:35,590 That's pretty much it. 37 00:02:35,700 --> 00:02:39,960 Now as a quick aside here obviously the best way to write out this code would have been to do something 38 00:02:39,960 --> 00:02:42,170 like this to initialize found word is false. 39 00:02:42,330 --> 00:02:44,820 And now typescript understands what's going on here. 40 00:02:44,850 --> 00:02:47,310 This is way better code than what we had before. 41 00:02:47,340 --> 00:02:51,870 But again I just wanted to give you a quick example of where we might have a declaration on one line 42 00:02:51,930 --> 00:02:54,090 and then initialization at some future point. 43 00:02:55,300 --> 00:02:55,520 All right. 44 00:02:55,540 --> 00:02:56,730 Let's take a quick pause right here. 45 00:02:56,740 --> 00:03:00,610 When we come back in the next video we're going to take a look at our final example of when we are going 46 00:03:00,610 --> 00:03:02,760 to prefer to use a type invitation. 47 00:03:02,860 --> 00:03:04,600 It's a quick pause and I'll see you in just a minute.