- The next two built-in types
of return type and parameters both deal with functions.
So to get started we just have a really simple function
that is going to showcase all these different things.
It's called check length,
takes in an A property
which is a string and a B property,
which is a number.
And if the length of the string is less than the number
we pass in it's going to return true, otherwise false.
And I'm just doing this
so we have a bunch of different types we can work with.
We have strings, we have numbers, and we have Booleans.
So if we check the signature of this
you can see the return type for it
is set to a Boolean right here.
Now, if we wanted to use the return type
of this function inside of our type, for example
I wanted to get a type which is return of length check.
Well, how exactly would I go about getting this?
I don't have any way right now to do that.
But with the return type function, I do.
Return type takes in a type
and this type must be a function
and it'll give you whatever that function returns
which in our case is a Boolean.
So you may think we just pass in check length.
That's not gonna work
because check length is a function and this expects a type.
So we need to get the type of our check length function.
And now if we cover over this
you can see the return of it is set to a Boolean.
The return type is really good
if we just want to get whatever a function returns.
If you have a function declared like this
as like JavaScript code
you need to make sure you get the type of it.
Otherwise, if you have a type that is an actual function
so we'll say type func equals
and this is just going to return to us void.
It's just a really simple function.
I could just pass that in right there
because it's just expecting a type. And if I hover over here
you can see it gives me void as that return type.
This is really useful
if you need to make sure you take the value
of a return type and use it somewhere else
in your code as like a parameter or another type.
Now another slightly less useful,
but still useful built-in type is the parameter type.
Let's say I wanted to figure out
what the type of the parameter A or B is in this function.
But what I can do is I can use the parameters type
and this parameters type takes in our function.
So we'll use check length in our case,
we'll type of check length just like that.
And now this is gonna be our params type.
And if we hover over this
you can see that it gives us an array.
And the very first thing in here is a string.
And the second thing is a number.
So it's like a two pull essentially
because it's saying we accept two parameters,
the first one is a parameter A, which is a string.
The second one is a parameter B, which is a number.
So if I wanted the very first parameter
I could do this and this should return to me a string.
As you can see, this is set to a string.
While if I wanted the second parameter, I'm over here
you can see now this is set to a number.
That's a really good way to essentially get a type based
on the parameters of a specific function.
And if the function has no parameters, for example
this func right here has no parameters,
you can see when I try to look at this
it's just going to be an empty array
'cause it's saying,
"Hey, there are no parameters at all for this function."
Now this is definitely less useful,
but sometimes if you're working in a library
where you need to pass along things to a function
but they don't have good types being exposed,
you may need to use this parameters type
to figure out what the actual type of that parameter is
so you can set that type inside of your code.
But like I said, parameters is much less useful.
But return type is something
you're going to be using all the time
because it's very common to type something
based on the return of another function.
And since everything in TypeScript is inferred by default
this Boolean is already inferred.
I don't need to manually set that anywhere.
So it's really good to be able to continue to infer it
by getting the return type of that specific function
without having to manually say, "Okay, you know what?
This here is a Boolean,
and then down here, this is also going to be a Boolean."
I don't have to worry about that
if I have all these different built-in types
that could just constantly build on the inferring,
which means I write less TypeScript,
but you still get all of the same benefits.