Time for another new file.
I'll rename apt yes here to Union aliases T.S. and add a new app.
Yes file where we can start from scratch again closed the other file and now in here I want to dive
a bit deeper into functions.
Now of course we already work with functions for example here in the union alias is file with the combined
function.
And for example you saw that for functions you can assign types to the parameters of functions.
Well turns out there is a bit more you can do with functions and types for that.
I'll recreate my add function which simply accepts numbers not strings which does one simple thing it
returns and one plus and 2.
So not too tricky.
Now as I mentioned we can't assign types to parameters.
That's not new.
D function overall however has one import our type.
It has a return type and here to return type is inferred by typescript we can see it if we hover over
at here at the end.
The colon after the parameter list this year describes the return type of the function so to type off
the return value.
Here I return the additional and 1 and 2 and since both these inputs are numbers typescript is able
to infer that the result of this will be a number and simply to return it that the return type of this
function therefore will be a number.
If you would concatenate them as strings here with two string for example then types could would correctly
infer that we return a string here.
Now of course we can always explicitly assign a return type here by adding a colon after dy parameter
list and then to type you want a return like number this would be the same type as inferred if it for
example set us to string.
I would get an error here because obviously my calculation here does not match my described return type.
Now just as with variables it's a good idea to let typescript do its job regarding type inference and
if you have no specific reason for explicitly setting the type you should therefore not set the type.
And instead let typescript infer to type regarding the return types.
Though there is one interesting type which we haven't seen before and which might be brand new to you
if you have no experience with our programming languages because it is a type that doesn't really exist
in JavaScript it's the white type.
Let's say we have the print or assault method.
There we get a no argument which should be.
Well no.
And then there I want to console log NUM and I want to console log it as part of a string where I say
a result plus numbers.
So now it is will be concatenated to one long string and will be output.
And now I can call print result and for example past two that the result of my add function call with
five plus let's say twelve.
So I am executing add here with two numbers and the result of that.
So what ad returns is then passed as an argument into print result and that should print it to the console.
If we now compile this and then have a look at that in our page or on our page here we see results 17
which makes sense.
Now the interesting thing here is the return type of print result.
What would you guess is the return type here.
So maybe you think it's string because I'm creating a string in here.
But keep in mind we're not returning this.
We're not returning anything here and data for disk print result.
Function here has a special return type white.
Now you might note is wide returned hype from other programming languages there you have something like
this javascript doesn't really know that or doesn't really have a name for this situation typescript
however does so indeed this function here has a return type of white.
And of course you don't need to specify as your type scripts inference did its job.
But I want to make it really clear that this has the wide return type which is why I am adding it here
for demo purposes.
Now why it really just means dysfunction doesn't have a return statement it doesn't return anything
it successfully completes its stuff it does its job and executes its code it does not yield an error
or anything like that but it then doesn't return anything.
That's the wide return type.
Now technically in JavaScript of course if we would console log the result of print result here and
I now compile this and we see what gets output here on the page we see that undefined is returned by
that.
So technically and that's re interesting in javascript if we use the return value of a function that
doesn't return anything we get undefined as a value.
And as you probably know undefined and Javascript is actually a real value a value you for example also
get if you try to access a property on an object which doesn't exist.
So here we get white even though technically this returns undefined.
And now to make it even more confusing undefined actually is a type in typescript you can have undefined
as a type.
And for example a brand new variable some value can receive undefined as a type and you will not get
an error.
This variable will now just be forever undefined how useful that might be as a different question but
undefined is a valid type and typescript nonetheless here you see we're getting an error and we are
getting an error because a function is not allowed to return undefined.
Technically it of course does but typescript thinks about functions a bit differently.
You should use why here if a function returns nothing and not undefined because with y you make clear
that this function deliberately does not have a return statement.
If you would say undefined here then typescript would expect that you have a return statement where
you just don't return a value.
That's the technical difference.
Now you see I'm not getting an error from a javascript perspective.
This is basically the same as this but typescript makes a differentiation here.
And therefore why does the type you want to use if you don't have a return statement undefined which
you will rarely need to be honest can be used if you return without returning an actual value.
But again this is a rare use case and you could use wide with a return statement like this as well you
would not get an error and by default you would actually also use white in this scenario unless you
really have a function that should produce undefined and you want to be clear about that.
But again dead will rarely be the case.
I just want to make sure here that you understand the difference between white and undefined that white
is the standard which you'll use in pretty much all scenarios where you have a function that does not
return a value and that you can assign it explicitly but of course types all always able to infer it
like it would be able here as well.
I just added this for demo purposes here.