Types of Function Headers

In this lesson, we will see three function signatures.

Function with parameters

A function’s signature can have a parameter that is a function too. These function’s parameters can have parameters at their turn but don’t require the invoker to use the parameters. A standard scenario is for an optional callback function.

For example, say you have a function that lets you specify an optional error callback named error of type Error. Not using the argument occurs when notification by the callback is important, but not the details from the argument. The reason is that behind it, JavaScript provides the value. The inspection of the arguments object, when not using the argument, corroborates this assertion. However, since you are not using the value, there is no need to clutter the code. TypeScript works in this dynamic fashion because of JavaScript. Since it doesn’t generate any potential error, TypeScript doesn’t enforce an unnecessary check.

On line 1, a function with two parameters is defined. The second parameter, named err, is a function callback. On line 2, the call to the function uses a function without the parameter: it is fine. Lines 3 and 4 have the parameter. The two invocations show that the name of the parameter is not required to be similar to the function.

Get hands-on with 1200+ tech skills courses.