Allow additional arguments or optional arguments for function calls in Typescript.
Consider the below example of a function that adds two numbers.
|
|
This works fine and as expected, but what happens when there is a discrepancy in number of arguments.
|
|
The above error is perfect.
Except for the fact that Javascript does not behave this way. So, we can mess up functions, modules and classes when incrementally migrating them to Typescript.
Optional arguments
Fortunately, Typescript has a work-around. You can make the last variable as optional.
|
|
We did not get compilation errors, but runtime errors continue to happen.
And, you guessed it - there is a work-around for that too. We just revert to the our old friend - argument defaults.
|
|
Additional arguments
Additional arguments supplied to the function throws error similar to the previous cases but with a complaint and loud houl about providing more than necessary.
|
|
We work-around the problem of additional parameters by taking some help from another old friend of the good guys - rest operator.
|
|