By 06/07/2015 2 Comments

TypeScript Function Overloading

Define Function Overloading:

  • Multiple functions/methods with the same name can be defined in TypeScript and that process is called overloaded functions.
  • In function overloading process, we can define multiple functions with the same name but their parameter-list might be different from each other by number, data types and sequence and their return type also would be different from each other.
  • Following are some rules to define overloaded functions in TypeScript and these rules are must be followed to implement function overloading in TypeScript:
  1. The functions which are being overloaded do not have their body/implementation, they have only declaration part.
  2. At the end of overloaded functions, another function with the same name must be defined with its body/implementation and it must be marked with “any” as its return type.

Example:

//Functions overloaded are declared with same name but are different in return type and parameter list.
function displayData(digit: number): number;
function displayData(techName: string): string;
function displayData(isChecked: boolean): boolean;

/* Another function with same name but with “any” as its return type and parameter list. */
function displayData(arg1: any): any {

    return arg1;
}

/* overloaded functions are called through its parameter types. */
document.write("digit: " + displayData(23) + " <br/> ");
document.write("Technology name: " + displayData(".NET") + " <br/> ");
document.write("isCheck: " + displayData(true));





Next Tutorial →



Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .