1 00:00:00,150 --> 00:00:02,010 - The next two built-in types I wanna talk about 2 00:00:02,010 --> 00:00:03,150 are complete opposite, 3 00:00:03,150 --> 00:00:05,730 and they are the partial and the required type. 4 00:00:05,730 --> 00:00:08,070 They just essentially allow you to create a type 5 00:00:08,070 --> 00:00:09,690 that either makes everything optional, 6 00:00:09,690 --> 00:00:11,190 or everything required. 7 00:00:11,190 --> 00:00:12,630 For example, I have this todo type here 8 00:00:12,630 --> 00:00:14,430 which has a title and a completed property. 9 00:00:14,430 --> 00:00:17,040 And let's say that I have a form for creating a todo. 10 00:00:17,040 --> 00:00:19,500 Obviously if I wanted those form inputs to be in a type, 11 00:00:19,500 --> 00:00:21,288 they would all be optional, 12 00:00:21,288 --> 00:00:23,250 because by default the form is completely blank. 13 00:00:23,250 --> 00:00:26,010 So to create essentially an optional version of this todo, 14 00:00:26,010 --> 00:00:27,300 I can create a brand new type, 15 00:00:27,300 --> 00:00:29,730 and we'll just call this FormTodo. 16 00:00:29,730 --> 00:00:31,890 And this is going to be equal to using our todo, 17 00:00:31,890 --> 00:00:36,390 but we wanna wrap it in that partial type, 18 00:00:36,390 --> 00:00:38,226 and we just pass in our todo, 19 00:00:38,226 --> 00:00:39,480 and now it makes every single key 20 00:00:39,480 --> 00:00:41,070 inside of this type optional. 21 00:00:41,070 --> 00:00:43,230 If I hover over this, you can see title and completed 22 00:00:43,230 --> 00:00:45,090 are now both optional. 23 00:00:45,090 --> 00:00:47,430 And this is super simple, it only takes one generic, 24 00:00:47,430 --> 00:00:50,130 so it makes the entire object optional. 25 00:00:50,130 --> 00:00:53,100 Or if we were to use required here instead, 26 00:00:53,100 --> 00:00:55,530 and these started out as optional for example, 27 00:00:55,530 --> 00:00:56,850 you can now see the FormTodo 28 00:00:56,850 --> 00:00:59,250 has everything set to required by default, 29 00:00:59,250 --> 00:01:01,920 and it removes all the optional things that you can do. 30 00:01:01,920 --> 00:01:03,810 So it just does it on the entire object, 31 00:01:03,810 --> 00:01:05,400 every single thing inside of it. 32 00:01:05,400 --> 00:01:07,260 And if I were to like, do some nesting, for example, 33 00:01:07,260 --> 00:01:09,630 I had an address with a street 34 00:01:09,630 --> 00:01:11,970 which is going to be a string here. 35 00:01:11,970 --> 00:01:14,670 And let's just make these different things optional as well. 36 00:01:14,670 --> 00:01:15,630 When I hover over this, 37 00:01:15,630 --> 00:01:18,030 you can see that it makes the address required, 38 00:01:18,030 --> 00:01:19,620 but it doesn't do deep nesting. 39 00:01:19,620 --> 00:01:22,260 So this street is still optional 'cause it's nested. 40 00:01:22,260 --> 00:01:24,660 It only does the top level keys. 41 00:01:24,660 --> 00:01:27,330 Now if I wanted to make only certain keys required 42 00:01:27,330 --> 00:01:28,230 and not other keys. 43 00:01:28,230 --> 00:01:29,940 Like I wanna make it so the title's required, 44 00:01:29,940 --> 00:01:32,250 but the address can stay as normal, 45 00:01:32,250 --> 00:01:33,720 well now I need to combine together 46 00:01:33,720 --> 00:01:36,060 the pick and omit keys with required. 47 00:01:36,060 --> 00:01:38,310 So for example, here I want to only make it 48 00:01:38,310 --> 00:01:39,900 so the address is required or I'm sorry, 49 00:01:39,900 --> 00:01:40,950 the title is required. 50 00:01:40,950 --> 00:01:43,860 So from my todo, I can pick specifically 51 00:01:43,860 --> 00:01:46,170 just the title property. 52 00:01:46,170 --> 00:01:48,344 Now by doing that, if I close this off, 53 00:01:48,344 --> 00:01:50,310 I can look here and I can say I get a form todo 54 00:01:50,310 --> 00:01:53,010 that just has a required title, super straightforward. 55 00:01:53,010 --> 00:01:55,500 But I wanna get all the rest of the properties here as well. 56 00:01:55,500 --> 00:01:58,320 So I can use an intersection to combine this together 57 00:01:58,320 --> 00:01:59,730 with my original todo. 58 00:01:59,730 --> 00:02:01,440 And now if I hover over this, you can see 59 00:02:01,440 --> 00:02:03,630 it's combining these two different things together. 60 00:02:03,630 --> 00:02:05,340 But that's going to be overriding some stuff 61 00:02:05,340 --> 00:02:07,620 'cause it's gonna be getting the title from here as well. 62 00:02:07,620 --> 00:02:09,720 So I want to omit the title from this one. 63 00:02:10,810 --> 00:02:13,320 And by doing this I'm essentially saying, okay, 64 00:02:13,320 --> 00:02:15,840 pick just the title and make that required. 65 00:02:15,840 --> 00:02:17,940 Then combine that together with everything else 66 00:02:17,940 --> 00:02:20,430 minus the title to give me what I had before. 67 00:02:20,430 --> 00:02:22,890 And now if we try to create something 68 00:02:22,890 --> 00:02:27,890 that has that FormTodo type, just like that, 69 00:02:28,260 --> 00:02:30,480 you'll see I can put a completed variable in here, 70 00:02:30,480 --> 00:02:32,160 so we'll say that is going to be true, 71 00:02:32,160 --> 00:02:34,140 and I can pass in a title which is whatever, 72 00:02:34,140 --> 00:02:36,330 and you can see our address is still optional, 73 00:02:36,330 --> 00:02:37,800 which is great, but our title is required, 74 00:02:37,800 --> 00:02:39,870 'cause if I remove that, I get an error because title 75 00:02:39,870 --> 00:02:41,760 is missing and it is required. 76 00:02:41,760 --> 00:02:43,373 And that's because of how I set this up right here. 77 00:02:43,373 --> 00:02:45,510 So it's very common that you're going to be seeing 78 00:02:45,510 --> 00:02:47,430 required and partial together 79 00:02:47,430 --> 00:02:49,350 with the pick and omit properties to make 80 00:02:49,350 --> 00:02:50,670 just certain things required 81 00:02:50,670 --> 00:02:52,770 and just certain things not required. 82 00:02:52,770 --> 00:02:54,060 It's so common that you may actually 83 00:02:54,060 --> 00:02:56,370 write your own helper functions that essentially 84 00:02:56,370 --> 00:02:59,610 do this line of code for you inside of like, your own type 85 00:02:59,610 --> 00:03:01,285 where you can have like, 86 00:03:01,285 --> 00:03:03,540 PickRequired or RequiredPick or something like that. 87 00:03:03,540 --> 00:03:05,910 You can make your own type to do that if you really want. 88 00:03:05,910 --> 00:03:08,130 Now the one nice thing about required, 89 00:03:08,130 --> 00:03:10,410 if we just remove this back to what we had before 90 00:03:10,410 --> 00:03:11,730 and we actually get rid of those omit 91 00:03:11,730 --> 00:03:14,070 and notice it also still works just fine. 92 00:03:14,070 --> 00:03:15,720 And that's because a required property 93 00:03:15,720 --> 00:03:17,640 is going to overwrite an optional property 94 00:03:17,640 --> 00:03:20,010 when you're combining together two types like this, 95 00:03:20,010 --> 00:03:21,960 but it will not work as well for partial. 96 00:03:21,960 --> 00:03:24,720 So for example, by default our title is required, 97 00:03:24,720 --> 00:03:27,000 and now we want to make that title optional. 98 00:03:27,000 --> 00:03:29,790 You can see here I need to make sure I omit the title 99 00:03:29,790 --> 00:03:32,190 from the second one because required properties 100 00:03:32,190 --> 00:03:34,740 always override non-required properties. 101 00:03:34,740 --> 00:03:37,140 Now you can see my title is optional in this, 102 00:03:37,140 --> 00:03:39,090 while when I wasn't doing this omit, 103 00:03:39,090 --> 00:03:40,830 the required title was overriding, 104 00:03:40,830 --> 00:03:42,540 so now you can see that title is required. 105 00:03:42,540 --> 00:03:44,550 That's why I'm getting this error down here. 106 00:03:44,550 --> 00:03:46,920 So if I wanted to write out a fancy type to do this for me, 107 00:03:46,920 --> 00:03:48,990 I'm gonna do it for required, for example. 108 00:03:48,990 --> 00:03:50,070 What I could do is I could say 109 00:03:50,070 --> 00:03:54,210 that we wanted to have a required pick. 110 00:03:54,210 --> 00:03:56,610 And this required pick is going to be a generic type 111 00:03:56,610 --> 00:03:58,740 that is going to take in whatever the thing is 112 00:03:58,740 --> 00:04:00,390 that I wanna check, that's just the default, 113 00:04:00,390 --> 00:04:01,710 T is what I'm going to call this. 114 00:04:01,710 --> 00:04:03,330 That's the object I'm going to be checking 115 00:04:03,330 --> 00:04:04,920 or the type I'm gonna be checking. 116 00:04:04,920 --> 00:04:07,350 And then I want to get all the keys from that. 117 00:04:07,350 --> 00:04:09,120 So I can say that I want to get K, 118 00:04:09,120 --> 00:04:10,290 which is gonna be one of our keys, 119 00:04:10,290 --> 00:04:11,730 you can call it key if you want. 120 00:04:11,730 --> 00:04:15,870 And that's going to extend the key of T just like that. 121 00:04:15,870 --> 00:04:18,030 So this is very similar to how pick normally works. 122 00:04:18,030 --> 00:04:19,950 This is the exact signature pick uses. 123 00:04:19,950 --> 00:04:21,510 Essentially it's saying we have a type, 124 00:04:21,510 --> 00:04:24,390 and then we have any of the keys from that type 125 00:04:24,390 --> 00:04:26,460 as the required pick section. 126 00:04:26,460 --> 00:04:28,290 Then I need to set that to an actual type. 127 00:04:28,290 --> 00:04:30,540 So we can use required here. 128 00:04:30,540 --> 00:04:32,400 We want to require a specific type, 129 00:04:32,400 --> 00:04:35,400 but we only wanna pick the specific keys from here 130 00:04:35,400 --> 00:04:37,770 that are in the section we called Key. 131 00:04:37,770 --> 00:04:40,380 So we'll call that Key, just like that. 132 00:04:40,380 --> 00:04:41,213 There we go. 133 00:04:41,213 --> 00:04:42,720 And now we have a required pick. 134 00:04:42,720 --> 00:04:44,550 So if I were to just extract this, 135 00:04:44,550 --> 00:04:47,370 bring my code back to where we had our actual todo type. 136 00:04:47,370 --> 00:04:49,680 And now what I can do down here is I can say, you know what, 137 00:04:49,680 --> 00:04:51,270 I wanna make our title required. 138 00:04:51,270 --> 00:04:53,010 So we'll make it optional up here. 139 00:04:53,010 --> 00:04:55,290 And then down here, or actually, sorry, 140 00:04:55,290 --> 00:04:57,570 I'm gonna put this type in so we can use it. 141 00:04:57,570 --> 00:04:59,287 Now I'm just gonna call RequiredPick, 142 00:04:59,287 --> 00:05:00,930 I'm gonna pass in my todo, 143 00:05:00,930 --> 00:05:02,550 I'm gonna pass in the thing we want to check, 144 00:05:02,550 --> 00:05:03,573 which is our title. 145 00:05:04,650 --> 00:05:06,330 Close that off, and now you can see, 146 00:05:06,330 --> 00:05:08,880 it's not quite working 'cause we need to make sure we also 147 00:05:08,880 --> 00:05:10,710 add in the union or sorry, 148 00:05:10,710 --> 00:05:12,570 the intersection of our type as well. 149 00:05:12,570 --> 00:05:13,860 Now if we give that a quick save 150 00:05:13,860 --> 00:05:14,760 and we hover over our todo, 151 00:05:14,760 --> 00:05:16,710 you can see property title is missing. 152 00:05:16,710 --> 00:05:19,170 So it's very common for you to write a required pick 153 00:05:19,170 --> 00:05:20,160 that looks like this. 154 00:05:20,160 --> 00:05:22,560 And if I wanted to do the same exact thing for partial, 155 00:05:22,560 --> 00:05:24,540 I could say PartialPick. 156 00:05:24,540 --> 00:05:25,500 And what that's going to do 157 00:05:25,500 --> 00:05:28,200 is it's just gonna swap this out for partial, 158 00:05:28,200 --> 00:05:30,060 and we need to make sure on our second one, 159 00:05:30,060 --> 00:05:32,730 we emit the exact same properties from our object 160 00:05:32,730 --> 00:05:34,410 right here, and we called this Key. 161 00:05:34,410 --> 00:05:35,250 There we go. 162 00:05:35,250 --> 00:05:37,600 But now if I wanted to make this a partial pick 163 00:05:39,210 --> 00:05:40,680 where the title starts out required 164 00:05:40,680 --> 00:05:41,820 and now becomes optional, 165 00:05:41,820 --> 00:05:44,160 you can see that that is working just fine. 166 00:05:44,160 --> 00:05:46,290 Now to real quickly recap what these lines of code 167 00:05:46,290 --> 00:05:48,360 are doing essentially, our required pick, 168 00:05:48,360 --> 00:05:49,830 we're saying that we have a specific type, 169 00:05:49,830 --> 00:05:51,360 in our case the todo type, it doesn't matter, 170 00:05:51,360 --> 00:05:52,980 just some type, we're going to be 171 00:05:52,980 --> 00:05:54,930 essentially requiring certain keys from. 172 00:05:54,930 --> 00:05:55,890 In order to get the keys, 173 00:05:55,890 --> 00:05:57,870 we're saying that this second property 174 00:05:57,870 --> 00:05:59,130 can be anything by default. 175 00:05:59,130 --> 00:06:00,960 If we just said key, it could literally be anything. 176 00:06:00,960 --> 00:06:04,050 But by extending the keys of T, we're saying, okay, 177 00:06:04,050 --> 00:06:06,930 this must be one of the keys in our type T. 178 00:06:06,930 --> 00:06:08,640 In our todo case, that means it has to be title, 179 00:06:08,640 --> 00:06:10,020 completed or address. 180 00:06:10,020 --> 00:06:11,850 You can see here I actually get autocomplete 181 00:06:11,850 --> 00:06:15,060 for address, completed and title, just like I would expect. 182 00:06:15,060 --> 00:06:16,530 Then what I'm doing is I'm saying, okay, 183 00:06:16,530 --> 00:06:19,110 we're going to require picking just from that type 184 00:06:19,110 --> 00:06:21,510 all of those keys and we're gonna be intersecting it 185 00:06:21,510 --> 00:06:23,250 with the initial type just to make sure 186 00:06:23,250 --> 00:06:25,500 we get all the other stuff from before as well. 187 00:06:25,500 --> 00:06:27,330 So it's taking all the stuff that I'm not picking, 188 00:06:27,330 --> 00:06:29,430 leaving it the same, and then the stuff I am picking, 189 00:06:29,430 --> 00:06:30,750 it's making required. 190 00:06:30,750 --> 00:06:31,920 It's just like the code we had before 191 00:06:31,920 --> 00:06:33,390 but this code is a little bit more condensed 192 00:06:33,390 --> 00:06:35,040 because we're just using T here 193 00:06:35,040 --> 00:06:36,603 instead of the full todo name.