function getStatsById(userId: string, timeWindow: TimeWindow, convertJSONOutput: true): Promise<IPlayerStats>;
function getStatsById(userId: string, timeWindow: TimeWindow, convertJSONOutput: false): Promise<IStatsItem[]>;
function getStatsById(
userId: string,
timeWindow: TimeWindow = TimeWindow.Alltime,
convertJSONOutput: boolean = true
): Promise<IPlayerStats | IStatsItem[]> {
调用时,将根据参数的值推断缩小的类型:
// Promise<IPlayerStats>
const a = getStatsById('', undefined, true);
// Promise<IStatsItem[]>
const b = getStatsById('', undefined, false);
true
或
false
boolean
// **** => ************
getStatsById(convertJSONOutput: true): Promise<IPlayerStats>;
我已经稍微修改了代码,这样我就可以创建一个独立的示例
TimeWindow
IStatsItem
,和
IPlayerStats
function getStatsById(userId: string, timeWindow: TimeWindow, convertJSONOutput: true): Promise<IPlayerStats>;
function getStatsById(userId: string, timeWindow: TimeWindow, convertJSONOutput: false): Promise<IStatsItem[]>;
function getStatsById(
userId: string,
timeWindow: TimeWindow = TimeWindow.Alltime,
convertJSONOutput: boolean = true
): Promise<IPlayerStats | IStatsItem[]> {
// Ommitted
}
// Promise<IPlayerStats>
const a = getStatsById('', undefined, true);
// Promise<IStatsItem[]>
const b = getStatsById('', undefined, false);