Function Overload in TypeScript

  • We can have a function with same name, but different parameters and different functionality. This is known as Function Overload.
  • We can first declare the functions and then define it.
  • If they differ on the number of parameters, we can mark the parameters as optional during function definition.
  • Please note that this function overloading is different from C# function overloading. There we used to have many function definitions with one function name; but here, we have only one function definition.
  • Based on our inputs, your code will choose which function to invoke.
  • We can differentiate our functions based on
    • Data Types of the parameters
    • Number of Parameters
    • Sequence of Parameters

Below is a sample code

function addition(num1:number, num2:number):void;
function addition(str1:string,str2:string):void;

function addition(a:any, b:any):void{
    console.log(a+b);
}

addition("Hello", " Friends");
addition(10,20);

Below is the output of above code:

Other Related Articles

Functions in TypeScript
Parameterized Functions – Part 1
Parameterized Functions – Part 2

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started