typescript function overloads code example
Example: function overload in ts
Overloaded Funcitons:
Set of different functions which have same name.
type Both = string | number;
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: Both, b: Both) {
if (typeof a === 'string' || typeof b === 'string') {
return a.toString() + b.toString();
}
return a + b;
}
const res = add('Joyous ', '21');
console.log(res);