[Typescript] Function Overloads

function reverse(str: string): string;
function reverse<T>(arr: T[]): T[];
function reverse<T>(stringOrArray: string | T[]): string | T[] {
  if (typeof stringOrArray === ‘string‘) {
    return stringOrArray
      .split(‘‘)
      .reverse()
      .join(‘‘);
  }
  return stringOrArray.slice().reverse();
}

reverse(‘Pepperoni‘);
reverse([‘bacon‘, ‘pepperoni‘, ‘chili‘, ‘mushrooms‘]);

Function overload doesn‘t compile to Javascript, it is just a way to tell typescript about type information

相关推荐