TypeScript Function Overloads Last Updated : 23 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. JavaScript function greet(person: string): string; function greet(person: string, age: number): string; function greet(person: string, age?: number): string { if (age !== undefined) { return `Hello, ${person}! You are ${age} years old.`; } return `Hello, ${person}!`; } console.log(greet("Alice")); console.log(greet("Bob", 30)); The function greet has two overloads: one with a single parameter person and another with person and age.The implementation checks if age is provided and returns an appropriate greeting.Output:Hello, Alice!Hello, Bob! You are 30 years old.More Example of TypeScript function OverloadsAdding Numbers or Concatenating Strings JavaScript function combine(a: number, b: number): number; function combine(a: string, b: string): string; function combine(a: any, b: any): any { return a + b; } console.log(combine(5, 10)); console.log(combine("Hello, ", "World!")); The combine function is overloaded to handle both numbers and strings, either adding or concatenating them.The implementation uses a single function to manage both scenarios.Output:15Hello, World!Fetching Data by ID or Query JavaScript function fetchData(id: number): string; function fetchData(query: string): string[]; function fetchData(param: any): any { if (typeof param === 'number') { return `Data for ID: ${param}`; } else { return [`Result for query: ${param}`]; } } console.log(fetchData(42)); console.log(fetchData("search term")); The fetchData function is overloaded to accept either a numeric ID or a string query.It returns a string for an ID and an array of strings for a query.Output:Data for ID: 42Result for query: search termCalculating Area for Different Shapes JavaScript function calculateArea(radius: number): number; function calculateArea(length: number, width: number): number; function calculateArea(...args: number[]): number { if (args.length === 1) { return Math.PI * args[0] ** 2; } else { return args[0] * args[1]; } } console.log(calculateArea(5)); console.log(calculateArea(10, 20)); The calculateArea function is overloaded to compute the area of a circle when given one argument and a rectangle when given two arguments.It uses rest parameters to handle a varying number of arguments.Output:78.53981633974483200Best Practices for Using TypeScript Function OverloadsDefine Clear and Specific Overloads: Ensure each overload signature is precise and unambiguous to enhance code readability and maintainability. Order Overloads from Most Specific to Least Specific: Arrange overloads so that more specific signatures appear before more general ones, aiding the TypeScript compiler in selecting the correct overload. Implement a Generalized Function Body: The function implementation should accommodate all defined overloads, using type guards or conditional logic to handle different parameter types appropriately. Comment More infoAdvertise with us Next Article TypeScript Function Overloads A akshitsaxenaa09 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League TypeScript Geeks Premier League 2023 +1 More Similar Reads TypeScript Functions Type TypeScript function types define the structure of a function, including its parameter types and return type, ensuring consistent and type-safe usage.Help validate the types of parameters passed to a function.Ensure the function returns the expected type.Improve code clarity and prevent runtime error 6 min read TypeScript Generic Functions TypeScript generic functions allow you to create functions that work with various types while maintaining type safety. By using type parameters, defined within angle brackets (<T>), generics enable functions to operate on different data types without losing the benefits of TypeScript's type-ch 3 min read Swift - Function Overloading In Swift, a function is a set of statements clubbed together to perform a specific task. It is declared using the "func" keyword. We can also set the return type of the function by specifying the data type followed by a return arrow ("->"). Syntax: func functionName(parameters) -> returnType { 8 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 Solidity Function Overloading Function overloading in Solidity lets you specify numerous functions with the same name but varying argument types and numbers.Solidity searches for a function with the same name and parameter types when you call a function with certain parameters. Calls the matching function. Compilation errors occ 1 min read Like