Skip to content

Instantly share code, notes, and snippets.

@leolanese
Created October 23, 2018 14:05
Show Gist options
  • Save leolanese/7394c698417782eaa210fd6537cb045e to your computer and use it in GitHub Desktop.
Save leolanese/7394c698417782eaa210fd6537cb045e to your computer and use it in GitHub Desktop.
Function overloading OOP
Function overloading OOP
// overload declaration
function sum(a: number, b: number): number;
function sum(a: string, b: number): number;
function sum(a: number, b: string): number;
function sum(a: string, b: string): number;
// overload definiton
function sum(a,b){
if(typeof a === "string"){
a = parseInt(a,10);
}
if(typeof b === "string"){
b = parseInt(b,10);
}
return a + b; // add to string (after parseInt them)
}
sum(1,2);
sum("1","2"); // send 2 strings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment