In the last video we spoke about two big issues in getting typescript mongers to work together.
We wrote out this model and everything looks good right now.
But as we saw as soon as we start to try to create a user we can start to run into some issues because
typescript is not checking the type of arguments we are passing to that constructor.
So this video we're going to figure out how we can solve issue number one.
How can we make sure that typescript is going to check the type of arguments we are passing into this
user constructor to solve this.
I'm going to go back over to my editor.
I can go to the top of the file right after the import statement.
I'm going to write out a new interface right here.
I'm going to put a comment on this interface just we understand what it is doing.
As we go into the future it's going to write out in and comment that says an interface that describes
the properties that are required to create a new user so in our case to create a new user you must provide
an email that's a string and a password that is a string.
That's what we want to have.
So I'm gonna write out an interface that describes this object right here.
I'm going to call this interface user adders adders is short for attributes.
So these are the attributes that are required to create a new user inside this interface.
I'll write out an email that has a string and a password that is a string.
We are now going to use this interface to make sure that each time we try to create a new user we are
passing in the correct set of attributes.
To do so we're going to use a little trick and this trick is going to be something that we're going
to keep in mind for all the rest of this application.
Anytime we tried to create a new user.
Let me show you what we're gonna do now at the very bottom.
Right after we create our new user or the actual user user model I'm going to define a new function
called Build user anytime that we want to create a new user.
We are going to call this function instead of calling new user.
So this is something this is a little trick.
This is the thing that you need to keep in mind all the Mongoose documentation that you're ever going
to look at or blog posts and so on are going to say oh you had to create a new document you called New
than the name of your model and you pass on the attributes we are not going to follow that because we
can not really do effective type checking with typescript if we follow this pattern instead anytime
that we want to create a new user document.
We are going to call this build user function.
So that is the very important thing that you keep in mind going forward as the argument to this function.
I'm going to receive an argument called adders and we're going to put in a type here of user adders
and then inside all we're going to do is turn right around and return a new user that gets fed that
set of actors.
So we're really doing here is putting in this extra little step just to get typescript involved in this
process of creating a user because we are never going to write out new user directly inside of our code.
Instead we're going to call build user typescript is going to be aware of the set of arguments that
must be provided to this function typescript knows we have to pass in an object that has an email that's
a string and a password that's a string.
So now so long as we only ever create a user by using this function type scripts going to have our back
and make sure that we are providing the correct set of arguments.
Now we can try to test this out by writing some similar code to what I showed you just a little bit
ago when we were trying to call new user directly with a set of arguments that didn't make any sense
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
start to add in any additional arguments like let's say something like that right there typescript going
to jump in and say nope don't do that.
Awesome.
That is what we want.
We want typescript to jump in and tell us that we are making a mistake in creating a user if I ever
make a typo in a property name.
So for example if I accidentally type out pad once again typescript has our back and if we ever tried
to assign a property or provide a property that is the incorrect type type skips going to have our back
as well if I put in a type of number right here instead of a string as says hey sorry but this really
needs to be a string okay.
So that's it.
That is how we are going to solve issue number one.
We're going to make sure that we do not call new user directly at any point in time even though that
is what the official documentation says.
Instead we are going to use this build user function because this is how we can get typescript involved
in the process of making sure we are passing in the correct set of attributes so we probably want to
make sure that we export this function like so.
All right.
So now having said that having it repeated to you like four times this is how we going to create a user.
I do want to throw out a little kind of uncertainty here so right now we've created this new function
and now any time we want to create a user or kind of work with users in any way.
We now have to import two different things from this file that is not super convenient.
So even though this approach works right here I'm wondering is there some way that we can improve it.
Is there some way that we can still make building a new user or creating a new user at least somewhat
similar to what we were doing before so that we don't have to have this really high mental burden as
engineers and have to memorize.
Oh yeah.
I always need to import this buildings or function any what time I want to create a new user.
So I'm thinking although this definitely works maybe we can take a very similar approach but express
it in a slightly different fashion just to make it a little bit more convenient for us so that minds.
Let's take another pause right here.
We're going to figure out how to rewrite this slightly to make it just a little bit more convenient
to use.