In this video, we're going to dissect the add function just a little bit and get a better idea about
how type annotation 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 of 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 seems that we add them together.
It knows that adding two numbers together results in a number, and then it seems 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 return 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 are 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 revert it back over to A plus B.
Now, one really important thing to understand here, and 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.
And 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 add it 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, you're 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, the 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 am going to restore that to A plus B.
Now, the 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 Colen no colon, no 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'm going 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 type script read the
body of our function and it knows that we are going to return a number.
So just like we saw with 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 going to take two arguments, 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, OK.
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 and B and then we have a
return value, a void, which means that we are not returning anything at all.
So TypeScript has used 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 no, 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 a 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.