So you can use types for function parameters and for the return value of the function.
Now to take it to the next level what after all the were a function type itself.
Now let me make it clear what I mean.
Let's say we have a variable.
Combine values and this variable by default has a type of any.
Now as you learned any is not that useful.
What I want to do eventually is I want to set combined values equal to add.
So when a store that at function a pointer at this add function in combined values so that in the end
we can console lock combine values and execute combined values as a function to which I pass eight and
eight.
Now if I save that and I execute this After compiling it so if we compile this file and then we let
this reload we get 60 here.
So that works and we would expect it to work because that is normal javascript code.
We can store a pointer at a function in a number variable and then deal for execute this variable as
a function because well it points at that function right.
So we can execute that function through that variable.
The problem we have with this snippet here from a typescript perspective just is that combined values
as any.
So if I set combined values to a number here thereafter of course we can comp. is unfortunately because
typescript has no chance of detecting that this is unwanted or that this could cause a problem.
But at runtime you would get an error because obviously we tried to execute combine values as a function
when it actually is a number.
Now we want to widen this and for that we need to be clear that combined values will hold a function.
Now a first step into that direction is that we set the type here to function.
Function is a type provided by typescript in the end.
And this makes it clear that whatever we store it in here has to be a function.
Therefore here I'm getting an error because 5 is a number and not a function.
And if I comment this out we can comp. is otherwise who would get an error during compilation and now
it is again runs as expected.
So that's good but it's not perfect because now we say this should be a function but it could also set
combined values equal to print result for example here.
And of course typescript would not complain because print result is a function but of course it's not
a function that takes two arguments.
So again if I compiled this typescript will not complain but if we reload we see undefined here and
result eight.
So I don't get the result I want because I store the wrong function in there.
Again it would be nice if types would tell us about that typescript can't inform us about this because
all we said to typescript is dead.
We want to store a function in there and does this clearly the case.
So it would be good if we could be more precise regarding how the function should look like that we
want to store it and combine values and that's where function types come into play function types are
types that describe a function regarding the parameters and the return value of that function.
A function type is created with this arrow function notation you know from JavaScript or at least close
to that notation.
You don't add curly braces here because we aren't creating a arrow function here.
We're creating a function type instead.
Now on the right set of this arrow you specify the return type of the function you eventually want to
be able to store here.
That should be no now with dad was saying combined values accepts any function that takes no parameters
and then returns a number.
Now that's of course not entirely what we want.
We want to make sure that combined values takes a function or is able to store functions that take two
numbers and return a number.
Now for Dad we can add parameters here to this function type.
We don't have to match the parameters names for up there just the types.
So we could have a which should be of type number and B which should be of type number.
And now what we're saying to typescript is that combined values should accept any function that takes
two parameters where each parameter is a number and where the function overall then returns a number.
And that's why typescript does not complain about us storing.
Add in combined values because add is a function that perfectly satisfies this type definition but it
does complain about print result because print resolved as it tells us here is a function of type 1
argument which is a number nothing is returned whereas we actually expect to get a function with two
arguments where each argument is a number and we all return a number.
So we have a mismatch here and if I would try to comp. is we therefore would get an error here of course
in the compiler and we only can fix this by removing this line of code.
Now we are able to recompile does without errors and now does runs as expected.
So function types allow us to describe which type of function specifically we want to use somewhere
B that an expected value in a parameter for create a function with some callback or like here a variable.