TypeScript function declaration and parameters
In TypeScript, a function is a block of code that can be called repeatedly. Functions accept parameters, perform operations, and return values. A function can be defined with specific parameter types and return type so that type checking occurs at compile time.
A function can be defined using the function keyword. The definition includes the function name, a list of parameters, and the function body. The function name is used to identify the function; the parameter list specifies which arguments the function accepts; and the function body contains the actual code. The return type of the function can be specified with a colon.
For example:
function add(a: number, b: number): number {
return a + b;
}
In this example, the name of the function is add. It accepts two parameters, a and b, both of type number, and its return type is also number. The code in the body adds the two parameters together and returns the result.
Function Invocation
Invoking a function means executing it by using its name within the code. When calling a function, you must pass arguments that match the types and quantity specified for the parameter list of the function definition. Upon invocation, the code inside the function's body is executed, after which the function returns a value.
For example:
let result: number = add(2, 3); // Call the add function with parameters 2 and 3
console.log(result); // Output 5
Optional Parameters and Default Parameters
In TypeScript, functions can define optional parameters and default parameters.
Optional Parameters
Optional parameters are marked using a question mark ? in the function definition. An optional parameter must appear at the end of the parameter list but cannot precede required parameters.
For example:
function sayHello(name?: string) {
if (name) {
console.log("Hello, " + name + "!");
} else {
console.log("Hello, world!");
}
}
sayHello(); // Output "Hello, world!"
sayHello("John"); // Output "Hello, John!"
In this example, the name parameter is marked as optional. Therefore, it can be omitted when calling the function. If no argument is provided, the function outputs a default greeting; if an argument is provided, the function outputs a greeting that includes the passed argument.
Default Parameters
Default parameters are marked using an assignment operator = in the function definition. They work similarly to optional parameters but can be omitted during invocation. If a parameter with a default value is omitted, its default value will be used instead.
For example:
function sayHello(name: string = "world") {
console.log("Hello, " + name + "!");
}
sayHello(); // Output "Hello, world!"
sayHello("John"); // Output "Hello, John!"
In this example, the name parameter is marked as a default parameter and assigned "world". If no argument is provided, the function outputs the default greeting; if an argument is provided, it uses that value.
Rest Parameters
TypeScript allows functions to accept an arbitrary number of arguments using rest parameters syntax. A rest parameter represents an array containing all extra arguments passed during invocation.
For example:
function sum(...numbers: number[]): number {
let result: number = 0;
for (let num of numbers) {
result += num;
}
return result;
}
console.log(sum(1, 2, 3, 4, 5)); // Output 15
In this example, the sum function uses rest parameters to accept an arbitrary number of arguments. The code in its body iterates over all passed-in arguments and adds them together before returning their sum.
Function Overloading
Function overloading allows TypeScript users to define multiple function signatures so that different operations can be performed based on parameter types and quantities. When a function is called, TypeScript selects the correct implementation by analyzing the type and quantity of the provided parameters.
For example:
function add(x: number, y: number): number;
function add(x: string, y: string): string;
function add(x: any, y:any): any {
if (typeof x === "number" && typeof y === "number") {
return x + y;
} else {
return x.toString() + y.toString();
}
}
console.log(add(2, 3)); // Output 5
console.log(add("Hello, ", "world!")); // Output "Hello, world!"
In this example, two function signatures are defined: one accepts two number-typed arguments and returns a result of type number, while the other accepts two string-typed arguments and returns a result of type string. When calling the add function, TypeScript automatically selects the correct implementation based on the types and quantity of passed-in parameters.