In this video we're going to dissect the ad function just a little bit and get a better idea about how
it type imitation works with functions.
So once again every time we add in an argument list we're always going to annotate every single argument.
Likewise right after that argument list we're going to add an annotation for what the function will
return and when we add in the return annotation right there typescript is going to immediately analyze
the body of your function.
So it's literally going to read the code inside there and decide whether or not you actually are returning
a value that is a number in this case typescript knows that A and B are numbers.
It sees that we add them together.
It knows that adding two numbers together results in a number and then it sees that we return that value.
So typescript can read this line of code right here and understand that we have done exactly what we
said we were going to do if we returned some other type of value from the function like a string like
so we'll very quickly get an error because now typescript has read the body of our function again and
seen that we are returning a string as opposed to the number we said we were going to do.
So if I hover over that it says specifically hey you're trying to return a String you're supposed to
return a number.
So I can reverted back over to a plus b no one really important thing to understand here.
This is kind of getting into one of the more interesting sides of types although we've declared what
type of value we are going to return typescript is not going to try to make sure we actually have the
correct code inside the function.
Now I know that sounds kind of weird but let me tell you what I mean by that.
You and I wrote a function here called add and so I think any engineer who looks at this function would
say OK if I call this it's going to add two numbers together and then we added in some annotation to
say oh yeah the function is going to return a number typescript can make sure that we are returning
the correct type of value but it does not actually tried to make sure that we have the correct logic
inside of our application or inside this function.
So in other words we could very easily return the difference between a and b.
So no longer are we adding we're now subtracting but typescript doesn't care about that.
It just says hey returning a number works for me.
Now this might seem like a really obvious thing but it's going to have some really big impacts on some
later topics we discuss.
Remember that type system is just concerned with types.
It doesn't actually vet your actual logic inside the function and make sure you're doing the right thing
so to speak.
All right.
I'm going to restore that to a plus b now.
Last thing I want to mention here is just a little bit more around annotations and type inference.
It's a quick diagram of this function all right.
Here we go.
So here's our ad function.
We have two arguments into it.
They are both numbers and we have a single output that is a number as well so when we think about the
arguments we have to always use type annotation.
In other words we have to write out colon no colon number right there every single time we define a
function.
There are some corner cases where we do not.
But right now we'll just kind of ignore those we'll discuss them later on.
So whenever you and I write a function in this course we are always going to add in these annotations
every time we write out a function so we do not get any type inference on arguments arguments we're
always going to add in those annotations.
However on the other side over here for the output from the function we do get the benefit of type inference
but we're not going to use it.
And so let me clarify what's going on there.
Back over inside my function I went to find the return value annotation right here and delete it.
If I now hover over add I'll see an annotation on the variable itself that says that ad is referring
to a function that takes two numbers and returns a number so that's type inference in play.
We did not add in a type return annotation but types could read the body of our function and it knows
that we are going to return a number.
So just like we saw with the inference before with variable declarations we have type inference around
only the return value from a function so we don't have to add in that return annotation if we don't
want to.
However like I said you and I always always always will and here's why.
Let's imagine we've got another function here called subtract and this is also gonna take two arguments
a and b they're both going to be numbers.
And I'm going to leave off the annotation.
So then inside of here we'll just subtract the two numbers like so and yeah okay.
I think that's it we're good to go so do you notice anything missing here.
Well I didn't put on a return statement so if I hover over subtract right now you'll see that we get
an annotation on here that says that we have two arguments as inputs a and b and then we have a return
value a void which means that we are not returning anything at all so typescript has use type inference
here to say hey since you're not returning anything from the function I guess you don't mean to turn
anything return anything from the function we made a mistake we did not return a value in typescript
has not done anything to tell us that we made a mistake inside of this function so if we instead add
in an annotation for the return type right here of number like so we'll very quickly see a error message
that would be our signal that hey we probably made a mistake inside the function and we should add on
a return like so so that's why you and I are always going to use return annotations is because we could
very easily make a mistake inside of a function and forget to return a value or even return an incorrect
type.
If we do so typescript is not going to give us an error it's just going to think oh that's what the
developer meant to do.
The only way that we're going to get an error is if we add on that annotation right there.
So like I said although we get inference on the output from my function we are not going to use it.
We're always going to add that annotation in all right.
So that's functions in a nutshell one or two quick more topics.
So quick pause and I'll see you in just a minute.