- Now one really common thing that you do in JavaScript
is you pass functions around to other functions
or you just deal with functions a lot in JavaScript
since it's a very function forward language.
But how exactly do you type something as a function?
In this case we have this sum function
we've been dealing with before
but this is one that specifically takes in a callback.
This cb is a callback function that we just pass
whatever a + b is to that callback function.
So how would we go about writing the type for this?
You may just think it's as simple as saying
that this is a function,
and that would be all you need to do.
And technically that's going to work as you can see here.
But function is not the way that you want
to define what a function looks like.
For example, if I come in here and I call this
with 1, 2, and a function inside of here,
which will just say console.log the sum,
and sum is coming directly from here.
There we go, you can see I'm getting a bunch of errors
'cause it doesn't know that this has a parameter.
As you can see, this implicitly has a type of any
so it doesn't know what this sum variable is.
That's 'cause this function is just a generic thing
that says it's a function,
it can take any number of any parameters
and it can return anything.
I have no idea, I have no information about it.
So if you wanna get more specific about what a function is
you essentially need to tell it
what that function looks like and it looks very similar
to how you write an arrow function.
So what we can do to define something
as a function is we can use these parentheses here to say
that this is where the parameters will go.
Then we'll use an arrow finally followed
by whatever the return type is.
In our case this callback function
is not returning anything at all.
So we can just say the return type here is void.
And as for the parameters
we know it's going to take one parameter.
We can call it whatever we want.
In our case, we know it's going to be a sum.
This is going to be a number.
So now we have typed this callback
by saying that this callback
is a function that takes one parameter.
That one parameter is a number
and it's going to return nothing, in our case void.
Now if we look at our code where we called it
you can see when we hover over sum,
it specifically has a type of number to it.
And if for some reason I tried to say
that this has multiple parameters, so for example param2,
now you can see I'm getting an error essentially saying
hey, there is no param2 in my callback type.
I would need to manually come in here
and add some other type onto the end of here
saying that there is a second parameter.
Now the one really nice thing about JavaScript
is you can call a function with any number of arguments.
So here, if I wanted to not care about the sum argument
I could just remove that argument.
And as you can see, it's still going to work fine.
You just need to make sure you pass the number of parameters
to the function or less, just like normal JavaScript works.
So here you can see I can take in that sum function
if I want, which of course in this case I do.
So anytime you're typing a function
it looks just like an arrow function.
We can show another example where we just create a type
which is just going to be a PrintNameFunc.
There we go, to integrate this type
all we need to do is come in here.
We know that this is going to return void.
We'll say void, and this takes in a name
which is a type of string, just like that.
Now if there are no parameters
you just leave this completely blank.
So this would be a function that takes no parameters.
So for example, a function that looks like this,
there we go, that would follow this exact signature up here.
If we gave it that name parameter, it would look like this
down here in a full fledged function.
There we go, these have the exact same type.
And if I wanted to return something
like a number for example, as you can see,
this is going to follow that exact same type signature.
If I hover over F, you can actually see
the signature is exactly the same for F.
You can see it says function F
and it says the name is a string
and it's returning a number.
So it's essentially the exact same thing.
The only difference is when you're defining the return type
in a function like this, you do it with a colon
followed by saying that this will be a number.
But when you're defining something as a function,
so like I want to use a function as a type,
you just do it with an arrow syntax like this.