In this video we're going to show you a slightly better way of riding out this ability is our function
so that's not a separate standalone function.
Instead we're going to get this thing built in or included with our user model the end goal here.
What we're really going forward is to kind of cut to the chase.
What I really want to do is used to say user dot build and then pass in an email and a password.
The nice thing about this approach.
If we could get it to work we no longer have to export a separate thing from this file and create a
new user is kind of a natural thing we say user built.
Build me a new user create a new one.
It makes a lot of sense and it possibly even makes more sense than this approach over here where we
say new user it's just all a bit easier to understand what is going on because we've got that build
method named in there.
So that's the refactor or the change we're going to make we're going to figure out how to build in or
include this build user function on the User model itself so let me show you how we do that to add in
a new method to our user model.
So really that thing right there we're going to take a look back at our user schema right above where
we create the new user or the User model.
I'm going to write out user schema dot statics dot built and then I'm going to assign to that the same
function that we wrote down a little bit ago.
So this one right here.
So we'll say adders is user adders and then return new user with adders.
I'm now going to remove the old build the user and delete it from the expert list as well.
So this is how we get a custom function built into a model we add it to this statics property on our
schema now it definitely appears like this works.
But let's try to actually use it.
So a quick test right here I'll say user dot builds and sure enough I still end up getting an error.
So this is yet another point at which getting TypeScript and Mongoose to work together is just a little
bit awkward typescript does not understand what it means to assign a property to the statics object
if we were making use of JavaScript everything would work just fine and to be truthful with you.
This right here it is accurate code everything we have.
Totally accurate.
This is how we add a function to a use it to a model in Mongoose but typescript just doesn't understand
that we have to give a little bit more information to typescript.
So let me show you how we're going to fix this.
I'm going to go back up to the top of the file again.
I'm going to add in yet another comment and I'm going to say an interface that describes the
properties that a user model has.
So we're gonna write out an interface that's going to essentially tell typescript that there's going
to be a build function available on this user model.
We're going to say that there is a built function and we're gonna tell it what arguments are required
to call it as well I'm going to write out interface user model I'm gonna say extends Mongoose dot model
we're going to take a look at this existing interface right here I know there's an error there don't
worry about that we'll fix up in just a moment I want to take all the properties that already exist
on that interface I want to add them into this new one that we are creating and I want to add in some
new properties on top that we are getting an error from this right now to fix that error we're gonna
put in a set of angled brackets and then write any inside there we're gonna come back and discuss what's
going on with that just a moment but right now we'll just leave in any then on this interface we're
going to write in that build method.
And again this is going to tell typescript about the instance of the build method and what properties
it accepts inside of here.
We'll write out build it's going to take an argument called adders.
It must be of type user adders
and from that we are going to return right now.
Just any we're gonna come back and fix up that any as well in just a moment.
So now that we've got this interface put together typescript can understand about what a real user model
is.
All we have to do is really tell typescript about the existence of this interface so I'm going to go
back down to where we created our Mongoose model right here.
I'm gonna put a set of angled brackets after model I'm gonna write an any and then user model and as
soon as I do so you'll notice that the era at year changes typescript is no longer complaining that
we're trying to access a build property on user anymore.
Instead it's transferred the air to the set of really braces.
So now it's telling us hey if you want to create a new user or if you want to call this build method
you must pass in an email and a password.
Now if I put in an email of test at test dot com and a password of password those errors go away so
we've now told typescript about this additional property that the user model is going to have a definite
looks like it works out.
Now even though this works unfortunately we just introduced a bunch of kind of mystery syntax right
here right here and we've got that any right there as well.
So again it works but while we've introduced a bit more confusion and unfortunately this is kind of
that stuff.
As mentioned earlier we're getting the typescript and Mongoose to work together involves a little bit
of kind of crazy code.
It definitely works and we've got some awesome type checking included now but there's kind of a mental
cost so to speak.
Included with it in addition to that we have really only solved issue number one right now.
We have not really solved anything related to issue number two where we needed to somehow tell typescript
and Mongoose that the set of properties that we pass into creating a new user are different than the
properties that end up on a user OK.
So still a little bit to get through here.
Right now I'm going to delete that user dot build because I was just a quick test.
I'm going to save this file.
We're gonna take a quick pause and continue again in just a moment.