1 00:00:00,950 --> 00:00:03,320 Now that we've got a better understanding of what a type is. 2 00:00:03,380 --> 00:00:08,660 Let's dive a little bit deeper into the subject and then discuss why we care about types at all. 3 00:00:08,660 --> 00:00:11,990 All right so first off a couple examples of some very basic types. 4 00:00:12,800 --> 00:00:17,930 So anytime that we are working with a value that has a type of string we would expect to see a value 5 00:00:17,990 --> 00:00:19,690 that look like these right here. 6 00:00:19,700 --> 00:00:23,840 So if I have a value that looks like hi there that is a string. 7 00:00:23,840 --> 00:00:28,610 If I see a value that looks like a string with no characters that is also string and something it says 8 00:00:28,610 --> 00:00:34,770 today's Monday yup that's a string as well likewise if I've got some really small decimal number that's 9 00:00:34,770 --> 00:00:39,740 a number a negative number has a number and a really large number is a number as well. 10 00:00:40,820 --> 00:00:45,680 And the reason I breakout number right here is that in some other languages there are actually subtypes 11 00:00:45,680 --> 00:00:46,340 of numbers. 12 00:00:46,340 --> 00:00:51,910 So for example in other languages you might have a type to represent numbers that have a decimal or 13 00:00:51,920 --> 00:00:56,510 you might have another type to represent a number that is only an integer and therefore doesn't have 14 00:00:56,510 --> 00:00:59,710 a decimal in JavaScript or see me in typescript. 15 00:00:59,710 --> 00:01:05,800 We have nothing like that a number is a number regardless of whether it is positive or negative has 16 00:01:05,800 --> 00:01:12,250 a decimal doesn't have a decimal they're all numbers if we have a value of true or false we would say 17 00:01:12,250 --> 00:01:17,990 that its type is boolean and then remember we are not only restricted to these basic types right here. 18 00:01:18,100 --> 00:01:21,370 Every value in typescript has a type. 19 00:01:21,370 --> 00:01:27,760 So for example if we made a new Date object the resulting object from that would have a type of date 20 00:01:28,660 --> 00:01:35,410 and then if we had an object that had an I.D. a completed flag a title flag we would say that that is 21 00:01:35,410 --> 00:01:41,740 an object that has type to do and this one only applies if we were working in a code base that had defined 22 00:01:41,740 --> 00:01:42,670 this interface right here. 23 00:01:42,670 --> 00:01:46,440 Remember this interface defined a new type okay. 24 00:01:46,440 --> 00:01:49,310 So now we've seen some examples of different types. 25 00:01:49,320 --> 00:01:54,800 Let's talk about how all these types kind of relate to each other so in a world of typescript we have 26 00:01:54,800 --> 00:01:57,100 two different categories of types. 27 00:01:57,140 --> 00:02:03,650 We have primitive types and object types primitive types are all the very basic types that you might 28 00:02:03,650 --> 00:02:03,890 get. 29 00:02:03,920 --> 00:02:10,310 So like no strings boolean null undefined and then symbol and void are two others we'll talk about later 30 00:02:10,310 --> 00:02:17,100 on the other category we have access to are something called object types and these are essentially 31 00:02:17,400 --> 00:02:22,080 any types that you and I create or any other types that are built into language itself. 32 00:02:22,320 --> 00:02:26,860 So for example all the different functions that you and I create are going to have their own type. 33 00:02:27,030 --> 00:02:29,260 And those are a object type. 34 00:02:29,460 --> 00:02:32,180 Any class you and I create or is going to have its own type. 35 00:02:32,190 --> 00:02:38,280 Same thing with arrays and objects as well now why is there that the divide here. 36 00:02:38,330 --> 00:02:42,590 Like why are there two categories one called primitive types and one called object types. 37 00:02:42,590 --> 00:02:45,550 Well the true answer to this we won't quite see until later on. 38 00:02:45,560 --> 00:02:50,720 But just as a quick preview we say that there is a separate category called object types right here 39 00:02:50,750 --> 00:02:56,750 essentially because you and I can write some code that will essentially trick typescript into believing 40 00:02:56,750 --> 00:02:59,720 that one value is a different type. 41 00:02:59,720 --> 00:03:04,460 I know that sounds really confusing right now but essentially we can kind of sometimes trick the typescript 42 00:03:04,460 --> 00:03:06,840 compiler in a very good way. 43 00:03:07,190 --> 00:03:11,180 We can do that with object types only we can not do that with primitive types. 44 00:03:11,180 --> 00:03:13,860 So that's why we have these two separate categories. 45 00:03:14,030 --> 00:03:16,100 Again I know that's confusing to hear right now. 46 00:03:16,130 --> 00:03:20,630 I just want you understand why you will sometimes see these broken out into the two separate categories 47 00:03:21,830 --> 00:03:22,160 OK. 48 00:03:22,180 --> 00:03:27,640 Now that we understand a little bit more about types onto the last big question here why do we care 49 00:03:27,700 --> 00:03:29,080 about types at all. 50 00:03:29,890 --> 00:03:32,770 If I had to answer that I would say that there are two big reasons. 51 00:03:32,830 --> 00:03:38,260 The first one is that types are used by the types of compiler to analyze our code for errors. 52 00:03:38,260 --> 00:03:40,390 So think about the definition that we just gave for types. 53 00:03:40,390 --> 00:03:45,310 We just said that types are an easy way to refer to the different properties and functions that a value 54 00:03:45,310 --> 00:03:50,650 has to just keep that in mind for a second while you keep in mind I'm gonna flip back over my code editor 55 00:03:50,650 --> 00:03:53,630 and take a look at the code that we just wrote a little bit ago. 56 00:03:53,820 --> 00:03:56,800 So inside of here we defined a new type. 57 00:03:56,800 --> 00:04:02,800 We said that we had a new type inside our application called to do and a to do is only ever going to 58 00:04:02,800 --> 00:04:08,960 have the properties I.D. title and completed then down here on line twelve. 59 00:04:08,980 --> 00:04:14,950 We said that the variable to do was going to refer to an object of type to do. 60 00:04:15,130 --> 00:04:20,170 So that means that the object that this variable refers to is only ever going to have these properties 61 00:04:20,850 --> 00:04:23,910 the typescript compiler can use that information. 62 00:04:24,010 --> 00:04:29,760 So if we start to refer to some property on that to do variable that doesn't exist typescript is going 63 00:04:29,760 --> 00:04:35,800 to throw an error at US typescript knows that this variable refers to an object that has these properties 64 00:04:35,860 --> 00:04:37,450 and nothing else. 65 00:04:37,450 --> 00:04:42,400 So if we try to reference some other property there must be something wrong with our code in typescript 66 00:04:42,400 --> 00:04:46,490 can discover that and tell us that in the form of an error. 67 00:04:46,610 --> 00:04:49,780 That's why we generally care about types. 68 00:04:49,920 --> 00:04:56,690 The other big reason that we care about types back over here one Mark is that allows it allows other 69 00:04:56,690 --> 00:05:00,670 engineers to understand what values are flowing around in our code base. 70 00:05:00,740 --> 00:05:05,270 If you've ever worked on a large javascript project before you might have ran into the scenario where 71 00:05:05,270 --> 00:05:10,910 you open up some file and you see some function in there with some really unclear argument name like 72 00:05:10,910 --> 00:05:15,500 maybe a function with an argument name of like the letter A and nothing else. 73 00:05:15,500 --> 00:05:20,450 In that case on a large project it might be really hard for you to understand what that function is 74 00:05:20,450 --> 00:05:28,090 doing if we instead tried to in annotate the type of arguments being passed into a function by labeling 75 00:05:28,090 --> 00:05:33,880 the types then that at least gives other engineers some idea of what type of data is flowing around 76 00:05:33,880 --> 00:05:35,340 inside of application. 77 00:05:35,530 --> 00:05:39,550 And ultimately it will make it slightly easier for other engineers to understand what our code is doing 78 00:05:40,790 --> 00:05:43,080 so that's why we care about types. 79 00:05:43,100 --> 00:05:44,530 Let's now take a quick pause right here. 80 00:05:44,540 --> 00:05:49,280 When we come back the next video we're gonna start to take a look at some more code snippets to understand 81 00:05:49,310 --> 00:05:50,230 what's really going on. 82 00:05:50,540 --> 00:05:52,400 So quick pause and I'll see you in just a minute.