zoukankan      html  css  js  c++  java
  • [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

  • 相关阅读:
    from import 的认识
    模块初识
    eq方法
    hash介绍
    item系列
    析构函数
    serializers进阶
    APIView源码解析
    RPC协议
    面试题补充
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13774097.html
Copyright © 2011-2022 走看看