CodesJava

Easy learning with example program codes

typescript function overloading tutorial


A TypeScript function is a group of statements which is used for performing a specific task. It provides the facility of code re-usability.

Method overloading means more than one methods with the same name but different parameters. Parameters can be differing in types, numbers or order.

Ways to implement method overloading:

  • Parameters differ in types.
    function showDetails(s1:string):void; 
    function showDetails(num1:number):void;
  • Parameters differ in number.
    function showDetails(s1:string):void; 
    function showDetails(x:string, y:string):void;
  • Parameters differ in order
    function showDetails(num1:numer, s1:string):void; 
    function showDetails(s2:string, num2:number):void;

Typescript function overloading example:

function showDetails(name:string):void{
  console.log(name); 
} 
function showDetails(empId:number):void{
  console.log(empId); 
}
showDetails("Jai") 
showDetails(123);

Try it:

JS Bin on jsbin.com
 

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki