typescript duplicate function implementation code example

Example 1: duplicate function implementation

export { } //put it at the top of the ts file

Example 2: 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);

Tags:

Misc Example