1 00:00:00,870 --> 00:00:04,940 In the last video we spoke about two big issues in getting typescript mongers to work together. 2 00:00:04,940 --> 00:00:07,160 We wrote out this model and everything looks good right now. 3 00:00:07,190 --> 00:00:12,200 But as we saw as soon as we start to try to create a user we can start to run into some issues because 4 00:00:12,200 --> 00:00:16,350 typescript is not checking the type of arguments we are passing to that constructor. 5 00:00:16,460 --> 00:00:20,540 So this video we're going to figure out how we can solve issue number one. 6 00:00:20,540 --> 00:00:25,370 How can we make sure that typescript is going to check the type of arguments we are passing into this 7 00:00:25,430 --> 00:00:28,640 user constructor to solve this. 8 00:00:28,670 --> 00:00:30,250 I'm going to go back over to my editor. 9 00:00:30,380 --> 00:00:33,440 I can go to the top of the file right after the import statement. 10 00:00:33,440 --> 00:00:36,800 I'm going to write out a new interface right here. 11 00:00:36,800 --> 00:00:39,980 I'm going to put a comment on this interface just we understand what it is doing. 12 00:00:40,010 --> 00:00:47,260 As we go into the future it's going to write out in and comment that says an interface that describes 13 00:00:47,260 --> 00:00:56,660 the properties that are required to create a new user so in our case to create a new user you must provide 14 00:00:57,590 --> 00:01:00,910 an email that's a string and a password that is a string. 15 00:01:00,920 --> 00:01:02,830 That's what we want to have. 16 00:01:02,840 --> 00:01:08,880 So I'm gonna write out an interface that describes this object right here. 17 00:01:08,950 --> 00:01:16,440 I'm going to call this interface user adders adders is short for attributes. 18 00:01:16,440 --> 00:01:21,210 So these are the attributes that are required to create a new user inside this interface. 19 00:01:21,210 --> 00:01:28,470 I'll write out an email that has a string and a password that is a string. 20 00:01:28,560 --> 00:01:33,130 We are now going to use this interface to make sure that each time we try to create a new user we are 21 00:01:33,130 --> 00:01:35,850 passing in the correct set of attributes. 22 00:01:36,130 --> 00:01:40,030 To do so we're going to use a little trick and this trick is going to be something that we're going 23 00:01:40,090 --> 00:01:43,160 to keep in mind for all the rest of this application. 24 00:01:43,160 --> 00:01:45,360 Anytime we tried to create a new user. 25 00:01:45,640 --> 00:01:48,300 Let me show you what we're gonna do now at the very bottom. 26 00:01:48,430 --> 00:01:54,310 Right after we create our new user or the actual user user model I'm going to define a new function 27 00:01:54,310 --> 00:01:59,980 called Build user anytime that we want to create a new user. 28 00:01:59,980 --> 00:02:05,920 We are going to call this function instead of calling new user. 29 00:02:05,950 --> 00:02:07,600 So this is something this is a little trick. 30 00:02:07,600 --> 00:02:12,760 This is the thing that you need to keep in mind all the Mongoose documentation that you're ever going 31 00:02:12,760 --> 00:02:17,590 to look at or blog posts and so on are going to say oh you had to create a new document you called New 32 00:02:17,790 --> 00:02:22,810 than the name of your model and you pass on the attributes we are not going to follow that because we 33 00:02:22,810 --> 00:02:28,900 can not really do effective type checking with typescript if we follow this pattern instead anytime 34 00:02:28,930 --> 00:02:30,660 that we want to create a new user document. 35 00:02:30,670 --> 00:02:33,480 We are going to call this build user function. 36 00:02:33,640 --> 00:02:41,030 So that is the very important thing that you keep in mind going forward as the argument to this function. 37 00:02:41,040 --> 00:02:46,860 I'm going to receive an argument called adders and we're going to put in a type here of user adders 38 00:02:50,650 --> 00:02:57,670 and then inside all we're going to do is turn right around and return a new user that gets fed that 39 00:02:57,670 --> 00:02:59,020 set of actors. 40 00:02:59,040 --> 00:03:04,270 So we're really doing here is putting in this extra little step just to get typescript involved in this 41 00:03:04,270 --> 00:03:10,090 process of creating a user because we are never going to write out new user directly inside of our code. 42 00:03:10,090 --> 00:03:15,160 Instead we're going to call build user typescript is going to be aware of the set of arguments that 43 00:03:15,160 --> 00:03:20,140 must be provided to this function typescript knows we have to pass in an object that has an email that's 44 00:03:20,140 --> 00:03:22,160 a string and a password that's a string. 45 00:03:22,240 --> 00:03:27,400 So now so long as we only ever create a user by using this function type scripts going to have our back 46 00:03:27,430 --> 00:03:30,760 and make sure that we are providing the correct set of arguments. 47 00:03:30,960 --> 00:03:34,800 Now we can try to test this out by writing some similar code to what I showed you just a little bit 48 00:03:34,800 --> 00:03:39,840 ago when we were trying to call new user directly with a set of arguments that didn't make any sense 49 00:03:41,550 --> 00:03:51,630 if I called build a user and pass in an object I'm going to throw in an email and a password if I now 50 00:03:51,630 --> 00:03:56,350 start to add in any additional arguments like let's say something like that right there typescript going 51 00:03:56,380 --> 00:03:58,770 to jump in and say nope don't do that. 52 00:03:58,770 --> 00:03:59,160 Awesome. 53 00:03:59,160 --> 00:04:00,110 That is what we want. 54 00:04:00,120 --> 00:04:07,210 We want typescript to jump in and tell us that we are making a mistake in creating a user if I ever 55 00:04:07,270 --> 00:04:08,800 make a typo in a property name. 56 00:04:08,800 --> 00:04:15,010 So for example if I accidentally type out pad once again typescript has our back and if we ever tried 57 00:04:15,010 --> 00:04:20,320 to assign a property or provide a property that is the incorrect type type skips going to have our back 58 00:04:20,380 --> 00:04:26,690 as well if I put in a type of number right here instead of a string as says hey sorry but this really 59 00:04:26,690 --> 00:04:28,980 needs to be a string okay. 60 00:04:29,020 --> 00:04:29,500 So that's it. 61 00:04:29,500 --> 00:04:32,250 That is how we are going to solve issue number one. 62 00:04:32,410 --> 00:04:38,080 We're going to make sure that we do not call new user directly at any point in time even though that 63 00:04:38,080 --> 00:04:40,130 is what the official documentation says. 64 00:04:40,150 --> 00:04:45,340 Instead we are going to use this build user function because this is how we can get typescript involved 65 00:04:45,370 --> 00:04:51,900 in the process of making sure we are passing in the correct set of attributes so we probably want to 66 00:04:51,900 --> 00:04:56,070 make sure that we export this function like so. 67 00:04:56,130 --> 00:04:56,360 All right. 68 00:04:56,490 --> 00:05:01,550 So now having said that having it repeated to you like four times this is how we going to create a user. 69 00:05:01,620 --> 00:05:08,220 I do want to throw out a little kind of uncertainty here so right now we've created this new function 70 00:05:10,250 --> 00:05:13,910 and now any time we want to create a user or kind of work with users in any way. 71 00:05:13,910 --> 00:05:19,440 We now have to import two different things from this file that is not super convenient. 72 00:05:19,490 --> 00:05:25,110 So even though this approach works right here I'm wondering is there some way that we can improve it. 73 00:05:25,160 --> 00:05:29,960 Is there some way that we can still make building a new user or creating a new user at least somewhat 74 00:05:29,960 --> 00:05:35,090 similar to what we were doing before so that we don't have to have this really high mental burden as 75 00:05:35,090 --> 00:05:36,730 engineers and have to memorize. 76 00:05:36,740 --> 00:05:37,080 Oh yeah. 77 00:05:37,090 --> 00:05:41,550 I always need to import this buildings or function any what time I want to create a new user. 78 00:05:41,750 --> 00:05:47,180 So I'm thinking although this definitely works maybe we can take a very similar approach but express 79 00:05:47,180 --> 00:05:53,240 it in a slightly different fashion just to make it a little bit more convenient for us so that minds. 80 00:05:53,270 --> 00:05:54,700 Let's take another pause right here. 81 00:05:54,710 --> 00:05:59,420 We're going to figure out how to rewrite this slightly to make it just a little bit more convenient 82 00:05:59,420 --> 00:05:59,990 to use.