Syntax to create function overloading in TypeScript
Last Updated :
22 Jul, 2024
Function overloading is a feature in object-oriented programming where multiple functions can have the same name but different parameters. The parameters can differ in number, types, or both. This allows a single function name to perform different tasks based on the input parameters.
Syntax:
function function_name(parameter1 : data_type, parameter2: data_type) : function_return_type;
In the above syntax:
- Different functions can have different parameters of various data types.
- The constraint is that the functions must have the same name.
- The parameters count can differ, but the types must be explicitly defined.
Example:
Let's create an example to illustrate function overloading in TypeScript. We will create three functions:
- Two function declarations with different parameter types.
- One function implementation that handles the logic based on the input parameter types.
JavaScript
function addFun(a:string, b:string):string;
function addFun(a:number, b:number): number;
function addFun(a: any, b:any): any {
return a + b;
}
console.log(addFun("Geeksfor", "Geeks"));
console.log(addFun(30, 40));
// This code is contributed by Aman Singla...
Output:
Geeksfor Geeks
70
In the above example code, we declare function addFun() with string type of parameters and overload that function as a declaration of another second function with the same name with number type parameters and the last function should have function implementation and what type of value this function will return will decide on what values we are passing as a parameter to this function via the function call.
In the first function call, addFun() with string type arguments so the function will return string. In second function call, we pass addFun() number type arguments so function will return number. Thus, in order to perform function overloading, we should take care of all these above mention things.
Key Points for Function Overloading
- Function Declarations: These specify the different overloads with different parameter types.
- Function Implementation: This contains the logic to handle the different parameter types.
- Consistent Naming: The overloaded functions must have the same name.
- Type Checks: The implementation function should check the types of the parameters and execute the appropriate logic.
Function overloading in TypeScript allows you to create multiple functions with the same name but different parameter types. This makes the code more flexible and easier to manage by grouping similar operations under a single function name. By understanding the syntax and implementation of function overloading, you can effectively utilize this feature to enhance the functionality of your TypeScript programs.
Similar Reads
How to achieve function overloading in TypeScript ? In this article, we will try to understand some basic details which are associated with the concept of function/method overloading, further will see how we could implement function overloading in TypeScript. Let us first understand some basic facts involved in function/method Overloading. Function/M
2 min read
TypeScript Function Overloads TypeScript function overloads enable defining multiple signatures for a single function, allowing it to handle various parameter types or counts.Enhances type safety by ensuring correct argument handling.Improves code flexibility and readability.JavaScriptfunction greet(person: string): string; func
3 min read
Explain the arrow function syntax in TypeScript Arrow functions in TypeScript are implemented similarly to JavaScript (ES6). The main addition in TypeScript is the inclusion of data types or return types in the function syntax, along with the types for the arguments passed into the function.What is arrow function syntax in TypeScript?Arrow functi
3 min read
How to Create TypeScript Generic Function with Safe Type Matching ? In TypeScript, generic functions offer a powerful tool for creating flexible and reusable code that can work with various data types. However, ensuring type safety is crucial to prevent runtime errors and maintain code reliability. These are the following approaches:Table of ContentUsing Type constr
4 min read
Function Overloading With Generics In TypeScript TypeScript has the ability to overload functions which means multiple signatures may be defined for one single function, this comes in handy when you want a function to behave in a certain way based on the provided parameters. In this article, we will be looking at how function overloading is done u
4 min read
How to Create an Object in TypeScript? TypeScript object is a collection of key-value pairs, where keys are strings and values can be any data type. Objects in TypeScript can store various types, including primitives, arrays, and functions, providing a structured way to organize and manipulate data.Creating Objects in TypescriptNow, let
4 min read