Answers for "function overload in ts"

0

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);
Posted by: Guest on October-11-2020

Code answers related to "TypeScript"

Browse Popular Code Answers by Language