Search⌘ K
AI Features

Function Signatures and the void Keyword

Explore how TypeScript enhances JavaScript by providing strong typing for function signatures, preventing type errors, and ensuring reliable return types. Understand the role of the void keyword for functions that do not return values, helping you write clearer and safer code.

Function signatures in TypeScript

One of the best features of using type annotations in TypeScript is that they can also be used to strongly type function signatures.

To explore this feature a little further, let’s write a JavaScript function to perform a calculation as follows:

Javascript (babel-node)
// This function calculates the result of (a * b) + c
function calculate(a, b, c) {
// Return the result of (a * b) + c
return (a * b) + c;
}
// Log the result of calling the calculate function with arguments 3, 2 and 1
console.log("calculate() = " + calculate(3, 2, 1));
Calculate function with numbers

Here, we have defined a JavaScript function named calculate from lines 2 — 6 that has three parameters, a, b, and c.

Within this function, we are multiplying a and b, and then adding the value of c.

The result is correct, as 23=62 * 3 = 6 ...