1 00:00:00,650 --> 00:00:05,060 In this video we're going to write out a couple of code examples to understand how type annotations 2 00:00:05,390 --> 00:00:07,790 work specifically with variables. 3 00:00:07,790 --> 00:00:12,650 Remember a type annotation is going to be a little bit of code that we write to help typescript along 4 00:00:13,700 --> 00:00:17,440 the type annotation system is kind of at odds with type inference. 5 00:00:17,520 --> 00:00:22,170 So we're going to first focus on type annotations and then come along and understand how type inference 6 00:00:22,410 --> 00:00:23,520 comes into play. 7 00:00:24,190 --> 00:00:24,430 All right. 8 00:00:24,450 --> 00:00:28,810 So going to change on over to my editor and inside my project directory I'm going to make a new folder 9 00:00:28,810 --> 00:00:30,840 called annotations. 10 00:00:30,940 --> 00:00:35,710 We're going to eventually create separate files inside this directory that are all related to type annotations. 11 00:00:35,800 --> 00:00:40,840 So we're going to organize them all inside of here inside this file during went to make a new file called 12 00:00:41,110 --> 00:00:47,050 variables dot t s and then inside this file I'm going to write out my first variable declaration that 13 00:00:47,050 --> 00:00:49,020 has a type annotation. 14 00:00:49,030 --> 00:00:54,910 So I'm going to write out const apples is going to be a number I'm going to set that to five like so 15 00:00:56,390 --> 00:01:02,600 so the colon and the word number right after it is our type annotation you're going to see syntax that 16 00:01:02,600 --> 00:01:08,180 looks exactly like this in the vast majority of typescript code that you work with this type annotation 17 00:01:08,200 --> 00:01:14,390 is trying to tell typescript that we are only ever going to assign a value of type number to the variable 18 00:01:14,420 --> 00:01:20,570 of apples if I ever tried to assign some other type of value besides a number all very quickly see an 19 00:01:20,570 --> 00:01:21,780 error message. 20 00:01:21,800 --> 00:01:27,260 So for example if I update the five right here to true which is a boolean I'll see an error right away 21 00:01:27,800 --> 00:01:33,130 and it tells me that the type of true is not a sizable to a type of number. 22 00:01:33,130 --> 00:01:37,920 Now this is also going to work if I tried to update the value assigned to that variable as well. 23 00:01:38,200 --> 00:01:40,380 So if I update the cons keyword to let. 24 00:01:40,450 --> 00:01:43,240 Which will allow me to reassign apples. 25 00:01:43,450 --> 00:01:49,040 I can then reassign apples down here a value of 10 without any issue however if I tried to assign it 26 00:01:49,040 --> 00:01:50,970 some other type of value like a string. 27 00:01:51,020 --> 00:01:56,780 Once again I'll very quickly see an error and once again I see a message that says essentially you have 28 00:01:56,780 --> 00:02:02,540 a string and you're trying to assign it to a variable that's supposed to be a number not for the rest 29 00:02:02,540 --> 00:02:03,110 of this file. 30 00:02:03,110 --> 00:02:05,850 I want to show you some reassignments all over the place. 31 00:02:05,960 --> 00:02:10,400 So I'm going to continue using the left keyword even though in just about all the declarations we're 32 00:02:10,400 --> 00:02:11,370 going to add here. 33 00:02:11,450 --> 00:02:17,560 Usually I would be making use of the concert keyword instead type invitations like this right here can 34 00:02:17,560 --> 00:02:20,030 be used with any type of value. 35 00:02:20,050 --> 00:02:23,830 Remember we just spoke about some of the different types of values inside of typescript. 36 00:02:23,830 --> 00:02:32,450 So we've got no strings billions no void no undefined and functions arrays and so on so with every one 37 00:02:32,480 --> 00:02:38,030 of these different types of values we can use a type invitation let's write out a couple of examples 38 00:02:38,060 --> 00:02:43,040 just to understand how type notation works with each of these different types. 39 00:02:43,230 --> 00:02:43,740 All right. 40 00:02:43,770 --> 00:02:47,210 So in every case we'll write outlet and then a variable name. 41 00:02:47,220 --> 00:02:52,370 In this case I going to call my variable speed then a colon then the type of value. 42 00:02:52,440 --> 00:02:57,590 In this case I going to say it's going to be a string and I'm going to assign it a value of fast like 43 00:02:57,600 --> 00:03:02,370 so once again if I tried to assign this a value that is not a string. 44 00:03:02,370 --> 00:03:05,550 So in this case maybe a number I'll see an error and that's pretty much it. 45 00:03:05,580 --> 00:03:09,900 We're going to see that same pattern repeating every time we tried to assign it a different type of 46 00:03:09,900 --> 00:03:11,360 value. 47 00:03:11,380 --> 00:03:17,530 Let's take a look at a boolean as well so let's say maybe let has name is going to be a boolean of true. 48 00:03:17,530 --> 00:03:18,570 How about no. 49 00:03:18,790 --> 00:03:25,360 I'll make a variable called nothing much and I'll make that null and that's going to be assigned a value 50 00:03:25,360 --> 00:03:26,530 of null. 51 00:03:26,530 --> 00:03:32,030 Now this is kind of interesting right here it's our first example of where we have a value that has 52 00:03:32,030 --> 00:03:34,030 a name identical to its type. 53 00:03:34,220 --> 00:03:36,890 We're actually going to see that a couple of times in typescript. 54 00:03:36,890 --> 00:03:39,190 That's something that's a little bit difficult to grasp. 55 00:03:39,200 --> 00:03:44,120 Once you start getting into some more complicated topics we're gonna have several lectures later on 56 00:03:44,120 --> 00:03:46,840 to understand when we're working with something that's a value. 57 00:03:46,880 --> 00:03:52,410 And when we're working with something that is a type we could also try a new variable called something 58 00:03:52,410 --> 00:04:00,660 like say nothing and make it undefined like so now we can also use type notation with every other type 59 00:04:00,660 --> 00:04:02,760 of value as well like we just said. 60 00:04:02,760 --> 00:04:08,370 So I can use this with an instance of a class or with a built in object or just about anything you can 61 00:04:08,370 --> 00:04:09,580 imagine. 62 00:04:09,580 --> 00:04:12,460 So let's say that we're looking at maybe a Date object. 63 00:04:12,860 --> 00:04:18,480 I'll say I'll put a little section header here and I'll say built in objects so I can make a new variable 64 00:04:18,480 --> 00:04:25,330 called now I'll say that's going to be a type date and then I'll make a new date and assign it on the 65 00:04:25,330 --> 00:04:26,730 other side. 66 00:04:26,740 --> 00:04:30,220 So in this case we're once again doing the same thing we're doing up here we're saying that we're going 67 00:04:30,220 --> 00:04:36,670 to have a variable called now and we can only assign it a value of type date and then on the other side 68 00:04:36,730 --> 00:04:40,830 we're going to assign it an instance of a date object. 69 00:04:41,040 --> 00:04:41,300 All right. 70 00:04:41,300 --> 00:04:45,240 So now we're kind of getting the idea here let's take a quick pause when we come back the next section 71 00:04:45,480 --> 00:04:49,890 we'll take a look at some of the more complicated syntax that comes up when we start working with objects 72 00:04:49,950 --> 00:04:51,300 arrays and functions. 73 00:04:51,420 --> 00:04:53,680 So quick posit I'll see you in just a minute.