1 00:00:00,960 --> 00:00:04,930 In this video we're going to look at the final case in which we will make use of type annotations. 2 00:00:04,950 --> 00:00:09,930 So this will be whenever we have a variable where its type can not be reasonably inferred even if the 3 00:00:09,930 --> 00:00:13,950 declaration and initialization are on the same line. 4 00:00:13,980 --> 00:00:16,990 This is really going to be a very big corner case right here. 5 00:00:17,010 --> 00:00:21,660 Nonetheless I just want you to understand when we want to use type annotations so let's try to get through 6 00:00:21,660 --> 00:00:26,640 this example really quickly going to flip back over to my editor and at the very bottom once again I'm 7 00:00:26,640 --> 00:00:32,340 going to add in a new section down here and I'll say this is the third case this is when we have a variable 8 00:00:33,310 --> 00:00:39,600 whose type cannot be inferred correctly for this example. 9 00:00:39,600 --> 00:00:42,600 We're going to create a array of numbers called numbers. 10 00:00:42,600 --> 00:00:48,450 I'm going to give it values like negative 10 negative 1 and twelve 0 then declare a variable called 11 00:00:48,510 --> 00:00:51,150 number above zero. 12 00:00:51,150 --> 00:00:54,240 So here's the idea I want to iterate through the numbers array. 13 00:00:54,420 --> 00:00:59,700 If we find a number that is greater than zero I want to assign it to number above zero. 14 00:01:00,050 --> 00:01:03,960 Otherwise I want to assign false to number above zero. 15 00:01:03,960 --> 00:01:09,000 So that means that we're going to have two different types of values being assigned to this variable. 16 00:01:09,000 --> 00:01:11,970 Now this is some kind of bad code or writing right here. 17 00:01:11,970 --> 00:01:14,460 I really don't recommend taking this approach in general. 18 00:01:14,460 --> 00:01:19,020 However there are scenarios where you might need to do something like this doing it with a number in 19 00:01:19,020 --> 00:01:23,280 a boolean doesn't make a lot of sense but there might be other cases like maybe you're trying to find 20 00:01:23,340 --> 00:01:30,120 a user's favorite piece of media and maybe possible choices for media are like a blog post or an image 21 00:01:30,360 --> 00:01:33,310 or a book or a movie or something like that. 22 00:01:33,330 --> 00:01:39,090 So if we had something like you know user's favorite media it would possibly make sense to assign some 23 00:01:39,090 --> 00:01:42,210 different type of values to this variable. 24 00:01:42,230 --> 00:01:44,910 So this is kind of where this scenario is going to come up. 25 00:01:44,930 --> 00:01:45,180 All right. 26 00:01:45,200 --> 00:01:48,810 So we're going to say that by default number above zero is going to be false. 27 00:01:48,980 --> 00:01:54,230 And then if we ever find something above zero inside this rate we need to assign that number to number 28 00:01:54,230 --> 00:01:54,900 above zero. 29 00:01:54,950 --> 00:02:03,630 So let's write this code out I'll say for what I equal zero I'm less than numbers dot length I plus 30 00:02:03,630 --> 00:02:04,710 plus. 31 00:02:04,830 --> 00:02:12,210 And then if numbers that I is greater than zero will say no above zero is going to be that number. 32 00:02:12,300 --> 00:02:14,090 So that's Give me numbers at I. 33 00:02:14,190 --> 00:02:16,320 As soon as we put that in we're going to see an error message. 34 00:02:16,320 --> 00:02:18,900 And I definitely would expect to see this. 35 00:02:19,030 --> 00:02:22,180 So type inference already kicked in right here on this line of code. 36 00:02:22,350 --> 00:02:24,780 We assigned false to number above zero. 37 00:02:24,780 --> 00:02:29,190 So typescript very correctly thinks that this is supposed to be of type boolean. 38 00:02:29,190 --> 00:02:33,360 Then on this line of code right here we're trying to assign something that is definitely a number to 39 00:02:33,360 --> 00:02:34,190 that variable. 40 00:02:34,290 --> 00:02:40,330 So the area is that we're trying to assign a number to a variable that is supposed to reference a boolean. 41 00:02:40,350 --> 00:02:45,510 So this is one the downsides of type inference type inference kicked in here it did its job but it didn't 42 00:02:45,510 --> 00:02:51,830 really understand the intent of our code or what we're trying to do so because type inference is marking 43 00:02:51,830 --> 00:02:53,980 this variable as always be a being a boolean. 44 00:02:53,990 --> 00:02:56,090 Our code won't work as is. 45 00:02:56,090 --> 00:03:02,720 So to fix this we could add in a type annotation so we could add in something that says Colin and then 46 00:03:02,750 --> 00:03:05,910 boolean or no. 47 00:03:05,950 --> 00:03:09,470 Like so this character right here is the pipe it is none L.. 48 00:03:09,490 --> 00:03:16,240 It's not an I is a character right above the return key on your keyboard you can picture this as being 49 00:03:16,240 --> 00:03:17,850 like an overstatement. 50 00:03:17,860 --> 00:03:24,880 So this is saying that no above zero is going to be either a boolean value or it might be a number one 51 00:03:24,970 --> 00:03:26,500 of the two. 52 00:03:26,530 --> 00:03:32,080 So any time we need to express kind of like an or statement with our types we will have to use a type 53 00:03:32,080 --> 00:03:39,140 annotation type inference won't work correctly so by using this type annotation typescript now understands 54 00:03:39,140 --> 00:03:44,930 that we can assign a value of type boolean or a value of type number two is variable and if we mouse 55 00:03:44,930 --> 00:03:47,320 over that variable it'll be reflected right there as well. 56 00:03:48,340 --> 00:03:48,600 All right. 57 00:03:48,630 --> 00:03:54,220 Like I said this is kind of a bad code example but we could kind of easily imagine some other scenarios 58 00:03:54,220 --> 00:03:59,290 where we might have to express a single variable that has possibly different types and we definitely 59 00:03:59,290 --> 00:04:03,570 are going to add some code that looks like this in a future project in this course. 60 00:04:03,570 --> 00:04:08,580 So we'll see this kind of scenario of where we have to add in a type annotation but with they can have 61 00:04:08,590 --> 00:04:10,660 more practical approach. 62 00:04:10,660 --> 00:04:10,880 All right. 63 00:04:10,880 --> 00:04:11,560 So that's pretty much it. 64 00:04:11,590 --> 00:04:15,940 So we now have taken a look at the three different cases where we definitely will have to use a type 65 00:04:15,940 --> 00:04:17,060 annotation. 66 00:04:17,080 --> 00:04:18,250 Let's now take a quick pause. 67 00:04:18,250 --> 00:04:23,650 We're gonna move on in the next video and talk about type annotations and inference around functions 68 00:04:23,710 --> 00:04:26,190 and objects as well where's that diagram right here. 69 00:04:26,200 --> 00:04:30,640 Functions and objects and are going to get through these other approaches really quickly because we 70 00:04:30,640 --> 00:04:35,530 just had to do some like really original work in this variable section so click pause and I'll see you 71 00:04:35,530 --> 00:04:36,400 in just a minute.