In this video we're going to get a better idea of what this angle bracket syntax is really doing for
us.
If you are already familiar with TypeScript and understand what generics are you can skip over this
video entirely.
Otherwise stick around the angle bracket syntax we see right here is the generic syntax inside a typescript.
You can really think of these generic things as essentially being functions or types.
Let me give you an example right now when we call model.
We've got a function invocation right here or the set of parentheses when we normally call a function
inside of typescript or JavaScript.
We pass in a set of arguments those arguments customize how that function behaves the same thing is
true or this list of generic type arguments as well.
You can really think of these as being some arguments to the function of model but instead of being
a data type or an actual value like let's say a string of user or this user schema thing it is instead
a type.
So again you can think of these as being types being provided to a function as arguments to really get
a better idea of what's going on behind the scenes.
I'm going to hold down command on my keyboard and then click on model if you're on windows you probably
want to hit control and then click instead that will take us to the definition of what the model function
is.
This is the typescript definition of that function not the actual javascript implementation so there
is a function called model and we can optionally provide to generic types to it.
So here is generic type number 1 and generic type.
Number two I know the syntax in here is really crazy.
All you really need to understand once again is that these re centrally arguments that we are providing
to this function they are not arguments in the same sense as normal arguments to a function that are
used to.
Instead they are customizing types so if we go back over to where we hold model we provided two types.
User dock end user model.
So those are going to show up as argument won and argument to so argument one is referred to as t that
named T right there is the same as though we had some kind of argument name such as name or schema.
We also have a second type argument that's going to be called you we can take a look at the function
definition and get a better understanding of how these different types are actually being used in particular.
Let's take a look at you right here.
You'll notice that that you type is being used down here at the very end remember that this syntax right
here is referring to the type of value that the function of model is going to return.
So this right here says that whenever we call model we're going to be returning type you.
And what is type you.
Well it's whatever we pass in as the second generic type argument to the model function in our case
our second type argument is user model and that means that we are saying that the model function over
all is going to return something of type.
User model and so if I actually go back over to and hover over user right here it says that user is
going to be uptime user model.
If I change the second argument right here to anything else then the model function is going to return
a value of type whatever I put in there.
So for example if I type in any now typescript the typescript thinks that the model function is going
to return a value of type any.
And so if I go and take a look at what gets assigned to user now user is of type any as well okay.
So going to revert that back over to user model so again this is referred to as generics in typescript.
This is a very brief very brief summary of what they're all about.
They essentially allow us to customize the types being used inside of a function a class or an interface.
We don't only need to understand a lot more beyond that right now.
I just want to give you a very quick understanding of what the stuff is being used for.
OK.
Let's take a quick pause here and continue in just a moment.