1 00:00:00,027 --> 00:00:01,920 - Now the next basic type I want you to learn 2 00:00:01,920 --> 00:00:03,930 is going to be the array type. 3 00:00:03,930 --> 00:00:06,060 So in order to understand how the array type works, 4 00:00:06,060 --> 00:00:08,220 let's just take a variable and assign it to an array 5 00:00:08,220 --> 00:00:10,710 and see what TypeScript infers the type as. 6 00:00:10,710 --> 00:00:12,270 So I can say const a equals, 7 00:00:12,270 --> 00:00:13,860 let's do an array of one, two, three. 8 00:00:13,860 --> 00:00:15,660 So this is just three different numbers. 9 00:00:15,660 --> 00:00:17,340 I hover over this, you can see that my type 10 00:00:17,340 --> 00:00:20,700 has been set to number with this square brackets at the end. 11 00:00:20,700 --> 00:00:23,130 And that's just a noting that this is a number array. 12 00:00:23,130 --> 00:00:24,960 So an array of numbers. 13 00:00:24,960 --> 00:00:26,850 So if I wanted to manually type this, 14 00:00:26,850 --> 00:00:28,590 I could say that this is going to be 15 00:00:28,590 --> 00:00:30,410 a number array just like that. 16 00:00:30,410 --> 00:00:32,310 So now if I were to move the default value, 17 00:00:32,310 --> 00:00:34,890 and I said that A is equal to for example, one, two, three 18 00:00:34,890 --> 00:00:36,330 you can see that works fine. 19 00:00:36,330 --> 00:00:37,530 Also I need to make sure I give this 20 00:00:37,530 --> 00:00:38,700 some type of default value, 21 00:00:38,700 --> 00:00:39,930 otherwise it's gonna gimme an error. 22 00:00:39,930 --> 00:00:42,810 'Cause undefined is not a number array. 23 00:00:42,810 --> 00:00:44,550 Also, I should probably use let here instead 24 00:00:44,550 --> 00:00:46,320 just to make sure all that's working. 25 00:00:46,320 --> 00:00:49,230 The really nice thing about manually defining these, 26 00:00:49,230 --> 00:00:50,400 is if I come in here with const, 27 00:00:50,400 --> 00:00:52,380 and I give this a value of like let's say one, two, three 28 00:00:52,380 --> 00:00:53,910 like we had before, 29 00:00:53,910 --> 00:00:56,100 and then I can do inside of here, is I can say dot push. 30 00:00:56,100 --> 00:00:58,500 And I say I wanna push a new value to the end of this. 31 00:00:58,500 --> 00:00:59,647 TypeScript is smart enough to know, 32 00:00:59,647 --> 00:01:01,410 "Okay, this is an array of numbers, 33 00:01:01,410 --> 00:01:04,050 so I can only add numbers to be added onto this." 34 00:01:04,050 --> 00:01:04,897 So I can only put numbers in here. 35 00:01:04,897 --> 00:01:06,660 If I were to try to put a string, 36 00:01:06,660 --> 00:01:07,950 you can see I'm going to get an error 37 00:01:07,950 --> 00:01:11,040 because I'm trying to add a string to an array of numbers. 38 00:01:11,040 --> 00:01:12,810 So this again, is where the power of TypeScript 39 00:01:12,810 --> 00:01:15,240 really explodes because by typing just one thing, 40 00:01:15,240 --> 00:01:16,500 and I don't even need to include the type 41 00:01:16,500 --> 00:01:17,790 'cause it's already inferred, 42 00:01:17,790 --> 00:01:20,040 you can see that I'm getting this awesome auto complete 43 00:01:20,040 --> 00:01:21,990 and this error protection in my code. 44 00:01:21,990 --> 00:01:23,640 And this code is just plain JavaScript. 45 00:01:23,640 --> 00:01:24,840 I haven't even written any TypeScript 46 00:01:24,840 --> 00:01:26,340 and I'm getting all of these awesome errors 47 00:01:26,340 --> 00:01:27,990 just by all of this inference. 48 00:01:27,990 --> 00:01:29,580 So if you want to type an array, 49 00:01:29,580 --> 00:01:31,260 just make sure that you're coming in here 50 00:01:31,260 --> 00:01:32,760 and you're doing whatever the type is. 51 00:01:32,760 --> 00:01:33,690 For example, a number 52 00:01:33,690 --> 00:01:35,850 followed by these square bracket syntax 53 00:01:35,850 --> 00:01:38,400 to denote that this is specifically an array of numbers 54 00:01:38,400 --> 00:01:40,350 and not just a single number. 55 00:01:40,350 --> 00:01:43,140 Now another thing to note is I have defined this const a 56 00:01:43,140 --> 00:01:44,460 as one, two, three. 57 00:01:44,460 --> 00:01:47,070 So you may think if it's following the literal type practice 58 00:01:47,070 --> 00:01:48,840 that this a would be typed as an array 59 00:01:48,840 --> 00:01:50,460 of values one, two, and three. 60 00:01:50,460 --> 00:01:52,650 I could manually type that out if I wanted to, 61 00:01:52,650 --> 00:01:54,570 by just saying one, two, three, like that. 62 00:01:54,570 --> 00:01:55,500 And now I'm saying that a 63 00:01:55,500 --> 00:01:57,810 must be the values of one, two and three, 64 00:01:57,810 --> 00:01:59,730 which means I can't push anything at all to it. 65 00:01:59,730 --> 00:02:01,200 If I try to push another number, 66 00:02:01,200 --> 00:02:03,120 for example, I push the value of four into here, 67 00:02:03,120 --> 00:02:04,440 you can see that I'm getting an error 68 00:02:04,440 --> 00:02:07,290 because four is not a value of three, one, or two. 69 00:02:07,290 --> 00:02:09,720 So the reason that I don't get this actual 70 00:02:09,720 --> 00:02:12,630 like explicit literal type when I define my array like this 71 00:02:12,630 --> 00:02:14,760 is because arrays just like objects 72 00:02:14,760 --> 00:02:17,940 they have a pass by reference as opposed to pass by value, 73 00:02:17,940 --> 00:02:20,370 which essentially means that I can call this push method 74 00:02:20,370 --> 00:02:22,050 which actually modifies my array 75 00:02:22,050 --> 00:02:23,850 without actually redefining it. 76 00:02:23,850 --> 00:02:26,910 Just like I would have to do with saying something like, 77 00:02:26,910 --> 00:02:29,130 a equals one, two, three, four. 78 00:02:29,130 --> 00:02:31,050 I can do the exact same thing as this code 79 00:02:31,050 --> 00:02:32,400 just by calling a.push. 80 00:02:32,400 --> 00:02:34,980 And that doesn't actually redefine the const variable. 81 00:02:34,980 --> 00:02:37,620 So the fact that this uses references instead of values 82 00:02:37,620 --> 00:02:38,940 and the fact you can modify a 83 00:02:38,940 --> 00:02:40,470 without actually redefining it, 84 00:02:40,470 --> 00:02:42,810 TypeScript says that this knows that it's a number array 85 00:02:42,810 --> 00:02:45,720 and not explicitly the values of one, two, and three. 86 00:02:45,720 --> 00:02:46,680 Which is really great because 87 00:02:46,680 --> 00:02:48,690 if it assumed the values were one, two, and three, 88 00:02:48,690 --> 00:02:50,910 it would almost always be wrong, since you're always 89 00:02:50,910 --> 00:02:53,040 pushing and removing elements from arrays. 90 00:02:53,040 --> 00:02:54,150 And the great thing is this works 91 00:02:54,150 --> 00:02:56,280 with all the different types you could possibly think of. 92 00:02:56,280 --> 00:02:57,870 For example, if we had an array of strings, 93 00:02:57,870 --> 00:02:59,760 you can see I get a string array here, here and so on. 94 00:02:59,760 --> 00:03:00,960 So all your different primitive types, 95 00:03:00,960 --> 00:03:02,340 and you can combine this together. 96 00:03:02,340 --> 00:03:04,710 For example, if I were to put dates inside of here, 97 00:03:04,710 --> 00:03:06,270 or if I just say, this is a new date, 98 00:03:06,270 --> 00:03:08,310 you can now see I have an array of dates. 99 00:03:08,310 --> 00:03:10,920 So specifically I could say that this is a date array 100 00:03:10,920 --> 00:03:13,080 just like that, and that's going to work fine. 101 00:03:13,080 --> 00:03:13,913 Now in this case, 102 00:03:13,913 --> 00:03:16,860 a date is essentially an object type inside a TypeScript, 103 00:03:16,860 --> 00:03:18,150 and we're gonna be covering that in a little bit. 104 00:03:18,150 --> 00:03:19,440 So it's not super important to know, 105 00:03:19,440 --> 00:03:20,550 but just know that you can use things 106 00:03:20,550 --> 00:03:22,170 that aren't primitives all over the place. 107 00:03:22,170 --> 00:03:23,820 So everywhere you could normally use a primitive 108 00:03:23,820 --> 00:03:26,120 you can use other things like dates and so on.