1 00:00:00,800 --> 00:00:05,090 In the last video we saw that we could remove all these different type annotations and we did not see 2 00:00:05,090 --> 00:00:06,110 any errors. 3 00:00:06,290 --> 00:00:11,330 In fact if we then hovered over these variables it's clear that typescript still understands what type 4 00:00:11,330 --> 00:00:14,210 of value we're going to assign to each given variable. 5 00:00:14,210 --> 00:00:19,700 So what you're seeing here is the type inference system in action in this video we're going to discuss 6 00:00:19,700 --> 00:00:24,920 how type inference works and then discuss when we're going to rely on type inference versus adding in 7 00:00:24,920 --> 00:00:26,390 an annotation manually. 8 00:00:26,390 --> 00:00:27,990 So let's get to it. 9 00:00:28,010 --> 00:00:30,740 All right so first off the basics of type inference. 10 00:00:30,960 --> 00:00:35,210 Anytime you and I make a new variable inside of our application we're actually undergoing two separate 11 00:00:35,210 --> 00:00:36,160 steps. 12 00:00:36,180 --> 00:00:41,420 So on the left hand side of this equals sign where we say const color we're doing something called variable 13 00:00:41,420 --> 00:00:42,770 declaration. 14 00:00:42,770 --> 00:00:46,370 This is where we are telling our program that we're going to have a new variable inside of application 15 00:00:46,370 --> 00:00:52,270 with the given name and then in the second step of this entire process on the right hand side the equals 16 00:00:52,270 --> 00:00:58,480 sign we are attempting to assign a value to that variable assigning a value to a variable for the very 17 00:00:58,480 --> 00:01:01,930 first time is referred to as variable initialization. 18 00:01:01,930 --> 00:01:05,290 So this entire line of code right here two separate steps first. 19 00:01:05,290 --> 00:01:12,720 Variable Declaration and then initialization here's the rule around type inference if we do our Declaration 20 00:01:12,780 --> 00:01:18,420 and initialization on the same line or essentially in one single expression then typescript is going 21 00:01:18,420 --> 00:01:23,090 to figure out the type of value that we're going to assign to color for us. 22 00:01:23,100 --> 00:01:27,270 So in this case typescript would look at the variable that were the value that we're usually using for 23 00:01:27,270 --> 00:01:33,360 initialization it would see that this is a string and it would say okay color must be a variable that's 24 00:01:33,360 --> 00:01:36,170 going to refer to a value of type String. 25 00:01:36,540 --> 00:01:42,780 And that's how we get this kind of pre done type right here where we can delete the annotation and typescript 26 00:01:42,780 --> 00:01:44,930 still sees what's going on for us. 27 00:01:45,270 --> 00:01:51,290 If I move this assignment or this initialization onto a separate line then it's not going to work anymore. 28 00:01:51,330 --> 00:01:56,520 So if I say apples equals five on the next line down by hover over apples you'll notice that it has 29 00:01:56,550 --> 00:01:58,930 a different annotation on here it says any. 30 00:01:58,950 --> 00:02:02,640 In this case we're going to discuss with any means and just a moment. 31 00:02:02,640 --> 00:02:07,050 But right now I think you can agree with me that typescript can no longer figure out what type of value 32 00:02:07,050 --> 00:02:08,940 we're going to assign to apples. 33 00:02:08,940 --> 00:02:14,400 So again the rule of thumb here is if for doing the initialization and declaration on the same line 34 00:02:14,610 --> 00:02:18,510 typescript we'll use type inference for us OK. 35 00:02:18,530 --> 00:02:20,870 So that's the easy part of type inference. 36 00:02:20,910 --> 00:02:22,010 We do everything on one line. 37 00:02:22,050 --> 00:02:23,900 Everything is going to work just fine. 38 00:02:23,900 --> 00:02:30,600 So now here's the really obvious question when are we going to add in these type annotations when are 39 00:02:30,620 --> 00:02:31,770 going to type them out. 40 00:02:31,950 --> 00:02:36,090 If we're saying that typescript can figure out these different types for us why would we go through 41 00:02:36,090 --> 00:02:38,780 the process of writing out the invitations manually. 42 00:02:38,910 --> 00:02:40,490 And that's a great question. 43 00:02:40,500 --> 00:02:45,540 Let's take a look at a diagram to understand why we might want to use type inference over annotation 44 00:02:45,780 --> 00:02:48,760 or the opposite. 45 00:02:48,790 --> 00:02:52,510 So in that file if we just put together we've got a lot of examples of type annotations. 46 00:02:52,570 --> 00:02:57,460 So remember that's where you and I the developers are going to tell typescript the type that a value 47 00:02:57,490 --> 00:03:02,170 or a variable is going to refer to now type inference. 48 00:03:02,170 --> 00:03:04,900 That's where typescript is going to guess everything for us. 49 00:03:04,930 --> 00:03:11,530 So in general we're going to rely on type inference all these all the time wherever we can we will rely 50 00:03:11,560 --> 00:03:12,920 on type inference. 51 00:03:13,240 --> 00:03:17,980 That means that whenever you and I have a variable that we are declaring and then initializing on the 52 00:03:17,980 --> 00:03:22,510 same line you and I in this course are not going to add and type annotations. 53 00:03:22,510 --> 00:03:27,190 So we're gonna have a lot of code that looks like this right here and we will much more rarely have 54 00:03:27,190 --> 00:03:31,770 code that looks like this down here or anything else that you see inside this file. 55 00:03:31,870 --> 00:03:35,910 So we are going to rely upon type inference as much as we possibly can. 56 00:03:36,220 --> 00:03:41,100 But there are three scenarios where we will rely on type annotations. 57 00:03:41,200 --> 00:03:44,440 So let's take a quick break right here and we're going to go through these three different examples 58 00:03:44,440 --> 00:03:49,240 of where we are going to add in these type annotations to help typescript out so quick pause and I'll 59 00:03:49,240 --> 00:03:50,760 see you in just a minute.