【发布时间】:2018-05-13 02:53:25
【问题描述】:
我有一个接受一个对象作为唯一参数的函数。例如:
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。我知道如何使用简单的布尔值来做到这一点,但是如何使用对象来实现呢?
【问题讨论】:
标签: typescript