Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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