0 votes
in TypeScript - JavaScript's Superset by
Does TypeScript supports function overloading as JavaScript doesn't support function overloading?

1 Answer

0 votes
by

Yes, TypeScript support function overloading. But the implementation is odd. When we perform function overloading in TypeScript, then we can implement only one functions with multiple signatures.

//Function with string type parameter    

function add(a:string, b:string): string;    

  

//Function with number type parameter    

function add(a:number, b:number): number;    

  

//Function Definition    

function add(a: any, b:any): any {    

    return a + b;    

}    

In the above example, the first two lines are the function overload declaration. It has two overloads. The first signature has a parameter of type string whereas the second signature has a parameter of type number. The third function contains the actual implementation and has a parameter of type any. Any data type can take any type of data. The implementation then checks for the type of the supplied parameter and execute a different piece of code based on supplier parameter type.

Related questions

0 votes
asked Jan 27, 2020 in TypeScript - JavaScript's Superset by AdilsonLima
0 votes
asked Mar 23, 2022 in TypeScript - JavaScript's Superset by sharadyadav1986
...