Typescript函数用对象重载

时间:2018-05-12 18:22:02

标签: typescript

我有一个接受对象作为唯一参数的函数。例如:

interface MyMap<T> {
  [id: string]: T;
}

type Options = {
  asObject: boolean,
  other?: Function
};

  function get(options: Options): any[];
  function get(options: Options): MyMap<any>;
  function get(options: Options): any[] | MyMap<any>;
  function get(options: Options = {asObject: false, other: () => {}}): any[] | MyMap<any> {
    if (options.asObject) return {} as MyMap<any>;

    return [];
}


const result = get({asObject: true});

当asObject值为true时,我想将类型推断为MyMap。我知道如何使用简单的布尔值来实现它,但是如何使用对象实现此目的呢?

0 个答案:

没有答案