![]() |
VOOZH | about |
TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.
There are several types of functions in TypeScript, which are listed below. We will explore these function types along with their basic implementations and examples.
In TypeScript, functions are defined and called by their name. They include types for parameters and return values.
function functionName([args: type]): type { }Output:
7An anonymous function in TypeScript is a function without a specific name, often defined inline, useful for one-off or small tasks where a named function isn't needed. The function call can be made using the variable to which it is assigned.
const variableName = function([args: type]): type { }Output:
3Arrow functions in TypeScript are concise function expressions using the => syntax. They retain the parent scope's this and are often used for short, simple functions.
const variableName = ([args: type]): type => expression;Output:
10Optional parameters in TypeScript allow you to specify function parameters that may be omitted when calling the function. Default parameters provide default values if no argument is passed.
function functionName(arg1: type, arg2?: type): type { }Output:
Hello, John Doe
Hi, Joe Smith
The return type in TypeScript specifies the data type a function should return. When we expect a function to return a particular type of value like either a string or a number, we can specify return types for functions.
function functionName(parameters: parameterTypes): returnType {
// Function body
return value; // Returns a value of 'returnType'
}
Example: Here is the basic example of Return type function in typescript.
Output:
16In TypeScript, the void return type indicates that a function doesn't return any value. It's often used for functions that perform actions without producing a result.
function functionName(parameters: parameterTypes): void {
// Function body
// No 'return' statement or 'return;' is used
}
Output:
Hello, Rahul!Rest parameters in TypeScript allow a function to accept a variable number of arguments of the same type, collecting them into an array for easy processing within the function.
function functionName(...args: type): type { }Output:
15Function overloading in TypeScript enables defining multiple function signatures for a single function, allowing it to accept different parameter types or counts while providing type safety.
function functionName(arg1: type, arg2: type): type;
function functionName(arg1: type, arg2: type, arg3: type): type;
function functionName(...args: any[]): any {
// Implementation
}
Output:
Hello, Anne!
Hello, John, you are 30 years old!
A callback function is a function that can be passed as an argument to another function and is executed when a specific event or task is completed. Callbacks are commonly used in asynchronous operations, such as handling the result of a network request or responding to user interactions.
type callBackType = (callBackFunctionName: type) => returnType;Example: In this example, two number type(a,b) parameters and a callback function(result) is passed to perform Operation function.
Output:
7