Kind of related to the spread operator ah rest parameters.
And for that all again tweak my add function.
Solid comment out all that code up here so that nothing breaks and re add my add function with any syntax
you want with the function keyword as an arrow function whatever you want.
But now I want to have a special function.
I don't want to restricted to two values I want to be able to add as many values as the user passes
in so that we can basically call ad like this 5 10 2 3 dot 7 ends on with as many values as you want.
If you would want to call it with 10 values that should also be possible.
Now of course however right now it's not possible as you can clearly see typescript is giving me an
error and correctly so because I'm accepting no arguments here.
Now of course I could accept for arguments here and assign the correct types.
But what if I then called it with 5.
As I said I want to be flexible.
That's where rest parameters come in in the place where you expect a list of values.
So not where you want to pass it but where you want to accept that as incoming values.
You can also use the free dots and then any name of your choice like numbers.
Now what this will do is it will merge all incoming parameters or generally the incoming list of values
comma separated list of values into an array.
So numbers here will be an array but of course by default a type of any values so an array full of any
values.
And that's not what we should do we should be more explicit in here we could say it will be a number
array like that.
So now we are defining the correct type and now we can work with that numbers array.
Now we can sum it up in a couple of different ways.
We can work with a result variable and then a for loop because numbers is in the right.
So here we could loop through numbers and add that to the result.
Or we use the reduced method which is available on arrays reduce works such that it performs an operation
on every element in an array.
Returns are assault and then adds these results together for Dad you provide a function to reduce and
then starting value which here is 0 and then dysfunction which you pass to reduce itself takes two arguments
and that's the current result you could say.
And it's the current value which we're looking at which will be each value off the array once and then
here you can return cur result which for the first run will be that initial value here plus curve value.
And if you do that reduce overall here we'll return a number.
So here we can overall return the result of numbers reduce and hence to add variable or constant here
will in the end hold a function which takes a couple of numbers not an array of numbers but because
of the risk parameters.
A list of numbers which is then merged into an array inside of the function and which returns a number
in the end.
So different now if we save everything it should all work and we see the result.
The sum of our little addition here.
So that's rest parameters.
A really useful feature for accepting an unlimited amount of arguments and a couple of built in methods
like the push method Brooke exactly.
That way you'll see push here has this strange parameter definition.
Well that's exactly what we just learned push here takes a couple of items for dessert array which is
an array full of strings.
It takes a couple of strings and this does not mean it takes an array of strings but instead here with
the rest parameters a comma separated list of strings which instead of portions then merged into an
array and handled internally.
That is the rest parameters syntax.
You can even combine this with tuples by the way if you know you want to support multiple arguments
but you know how many it will be.
So if it's not an unlimited list then you can also use a tuple type there and for example say I do accept
my free numbers here and then I have to remove the fourth argument here of course when calling a function
because now I'm pretty clear that I want to get free arguments which each are numbers.
So of course we could have just added free standalone parameters here but this syntax might still be
a bit shorter or easier to setup than adding the free parameters it really depends on your application
and your requirements here.
All move away from that setup and be more generic and accept as many arguments as I get because for
this function I am indeed able to handle an infinite amount of arguments.