Functions in TypeScript
Now that you're equipped with a detailed description of Simple Datatypes, Special Datatypes, Control Statements and other conecpts, it's time to put them to good use!
In this section, you will explore various function-related concepts such as functions, function types, optional parameters, default parameters, rest parameters, and function overloadings, and learn how to use them effectively in your TypeScript programs.
1. Functionsβ
Type annotations in functions ensure that the arguments passed to the function and the value it returns are of the expected type.
Example:
function add(a: number, b: number): number {
return a + b;
}
let result = add(5, 3); // 8
console.log(result); // Output: 8
2. Function Typesβ
Function types specify the parameter types and return type, allowing you to ensure consistency when assigning functions to variables.
Example:
let myFunction: (a: number, b: number) => number;
myFunction = function(x: number, y: number): number {
return x + y;
}
console.log(myFunction(2, 3)); // Output: 5
3. Optional Parametersβ
Optional parameters allow you to call a function without passing some arguments, using the ? syntax.
Example:
function greet(name: string, greeting?: string): string {
return greeting ? `${greeting}, ${name}!` : `Hello, ${name}!`;
}
console.log(greet("Alice")); // Output: Hello, Alice!
console.log(greet("Alice", "Good morning")); // Output: Good morning, Alice!
4. Default Parametersβ
Default parameters allow you to specify default values for function parameters, which will be used if no value is provided.
Example:
function multiply(a: number, b: number = 1): number {
return a * b;
}
console.log(multiply(5)); // Output: 5
console.log(multiply(5, 2)); // Output: 10
5. Rest Parametersβ
Use the rest parameters to handle an infinite number of arguments of a function. Rest parameters allow you to represent an indefinite number of arguments as an array.
Example:
function sum(...numbers: number[]): number {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
6. Function Overloadingsβ
Function overloadings allow you to define multiple function signatures for the same function, enabling it to handle different argument types and counts.
Example:
function combine(a: string, b: string): string;
function combine(a: number, b: number): number;
function combine(a: any, b: any): any {
if (typeof a === "string" && typeof b === "string") {
return a + b;
}
if (typeof a === "number" && typeof b === "number") {
return a + b;
}
}
console.log(combine("Hello, ", "world!")); // Output: Hello, world!
console.log(combine(10, 20)); // Output: 30
These concepts and examples illustrate how to effectively use functions and their advanced features in TypeScript, enhancing your ability to write robust and type-safe code.