代码之家  ›  专栏  ›  技术社区  ›  Shift 'n Tab

函数重载接口参数

  •  1
  • Shift 'n Tab  · 技术社区  · 7 年前

    我正在尝试使函数重载 search() 功能。此方法必须具有不同的搜索操作。

    1. 只搜索字符串类型。
      • search(key: string): IPagination<Employee[]>;
    2. 搜索 BasicFilter 类型:
      • search(x: BasicFilter): IPagination<Employee[]>;
    3. 搜索 PaginatedFilter 类型:
      • search(y: PaginatedFilter): IPagination<Employee[]>;

    我如何检查这个接口的类型 search(args: any): any; 方法?

    尝试执行以下操作:

      if (typeof args === BasicFilter) {
        console.log('searched with basic filter arguments');
      }
    

    TS消息: “basicfilter”只引用类型,但在此处用作值。

    错误消息: 未定义basicfilter


    以下是代码:

    接口

    interface PageData {
      pageIndex: number;
      pageSize: number;
    }
    
    interface Employee {
      id: number;
      name: string;
    }
    
    interface BasicFilter {
      key: string;
      is_archived: boolean;
    }
    
    interface PaginatedFilter {
      key: string;
      is_archived: boolean;
      page: PageData;
    }
    
    interface IPagination<T> {
      length: number;
      list: T;
    }
    

    班级

    class Service {
    
      constructor(public name) {}
    
        search(x: BasicFilter): IPagination<Employee[]>;
        search(y: PaginatedFilter): IPagination<Employee[]>;
        search(key: string): IPagination<Employee[]>;
        search(args: any): any {
    
          if (typeof args === 'string') {
            console.log('searched with keyword only');
          }
    
          if (typeof args === 'object') {
            console.log('searched with object arguments');
          }
    
        }
    
    }
    

    使用

    const service = new Service('Serbisyo publiko');
    
    service.search({ key: 'some key', is_archived: false });
    
    const default_page: PageData = { pageIndex: 0, pageSize: 15 };
    
    service.search({ key: 'some queries', is_archived: true, page: default_page });
    
    service.search('filtering data..');
    

    输出

    searched with object arguments
    searched with object arguments
    searched with keyword only
    

    DEMO

    2 回复  |  直到 7 年前
        1
  •  2
  •   Titian Cernicova-Dragomir    7 年前

    因为接口在运行时不存在,所以不能用类型来保护它们 typeof instanceof in page 作为一名优秀的候选人:

    search(x: BasicFilter): IPagination<Employee[]>;
    search(y: PaginatedFilter): IPagination<Employee[]>;
    search(key: string): IPagination<Employee[]>;
    search(args: BasicFilter | PaginatedFilter | string): any {
    
        if (typeof args === 'string') {
            console.log('searched with keyword only');
            args // is typed to string here
        }
        else if ('page' in  args) {
            console.log('searched with object arguments');
            args // is typed to PaginatedFilter here
        }else {
            args // is typed to BasicFilter here
        }
    }
    

    在里面

        2
  •  0
  •   Amit Saxena    7 年前

    interface IFilterCriteria {
    }
    

    这意味着它不保持任何状态。它只是一个标记接口

    interface BasicFilter extends IFilterCriteria {
        key: string;
        is_archived: boolean;
    }
    

    同样,我们可以修改

    interface PaginatedFilter extends IFilterCriteria {
        key: string;
        is_archived: boolean;
        page: PageData;
    }
    

    现在,搜索函数变得更加类型安全,其签名将是:

    search(args: IFilterCriteria): any;