In this video we're going to start to solve issue number two.
Remember this.
Issue number two was related to the fact that these set of properties that we pass in to create a new
user document are different than the properties that actually exist on that document.
Behind the scenes we might add in say a created at or in updated at property let's figure out how we're
going to solve this.
After that we'll then start to reflect on some that kind of strange syntax that we saw around that right
there.
And the any and the any right there OK.
So to solve this issue issue number two we're gonna create a another interface.
So after the two that we already put together I'm going to put another comment in here and say an interface
that describes the properties that a user document has these interfaces.
If you really sit down and read it they really make a lot of sense.
We've got one that describes what it takes to create a user.
We've got another one that describes what the entire collection of users looks like or least methods
associated with the User model.
And then an interface that describes what properties a single user has going to write out interface
we're gonna call this one user Doc and I can say extents Mongoose dot document so once again we're gonna
say that the definition of what a user document is.
Make all the different properties that a normal document has.
And we're going to add a couple more on top.
So in this case we're gonna say that an instance of a user is going to have an email that is a string
and a password that is a string
so those are the properties that a single user has.
If we ended up adding in additional properties or if Mongoose automatically added those properties in
this is where we list them.
So we would list out something like read it at.
That's a string and updated at that is a string as well.
But in our case we are not telling Mongoose to add in these properties for us right now.
So I'm going to remove those.
But again if we had those extra properties that is where we would add them
OK.
Now you put this together we're now going to take this interface and we're going to apply it down at
the very bottom
so where we are creating our user right here or the user model I can replace the any right there with
user Doc I'm also going to go back up
to where we defined the interface for user model.
So we've now got an interface that describes what a single user looks like which means when we call
built rather than returning any right here or saying that we are going to return any we're going to
instead indicate that we are going to return an instance of a user or a user document it's going to
replace that any with user Doc I'm going to replace the any right there with user doc as well.
So let's now go down to the very bottom and we'll do a quick test.
I'm going to once again create a new user by doing a user dot build and I'll pass in an email of whatever
and a password of whatever to now if I hover over user right here I'm being told that I got back an
instance of user duck it's now if I try to print out or access properties on here I can access things
like user dot email I can also access user dot password if we do not have this interface correctly set
up user dock right here we would not be allowed to access these properties safely.
So for example let's say that we actually did want to add in this idea of a created that time or created
or updated that time if we write out to user dot updated at typescript is going to complain and say
hey that property doesn't exist even if we know that that mongoose is going to add in this property
automatically for us.
So to tell typescript that hey it's OK if we want to access the thing oh you'd have to do.
Again go back up to the user dock interface and say updated at is going to be a string like so and if
we go back down now that error has gone away OK.
So that's it.
That's how we solve issue number two it's gonna delete this extra stuff right here I'm gonna go back
up to the interface and delete that.
Updated at and save the file.
We're going to take a quick pause right here.
When we come back in the next video one last thing I want to discuss what is the goal of these little
brackets right here.
So down on Mongoose top model also up here and I think that was the only other location.
So what's the goal of those little angled brackets.
This is going to be a lecture that's mostly just about typescript because this really is some typescript
syntax right here.
I want to give you a little bit of backstory on what the goal of those things are and why we had to
add them in.