Tutorial writing Typescript functions
Before we get into the tutorial of writing Typescript functions. First review the two ways of writing functions in Javascript…
Now we add type enforcement with Typescript …
Here we have “typed” the parameters and the return value. It works. However, we had not “typed” the “add” variable which is a function type. We do it now like this…
Since this is a bit verbose and some duplication, there is a shorthand form that works just as well…
Default Parameters
Here the second parameter on both function is optional and defaults to 0…
Here the second parameter is optional but has no defaults and hence function returns NaN because it can not perform the add/sum…
Here is an example of using rest parameters…