TypeScript 进阶
函数重载
function overloadExample(x: number, y: number): number;
function overloadExample(x: string, y: string): string;
function overloadExample(x: any, y: any): any {
if (typeof x === "number" && typeof y === "number") {
return x + y; // 数字相加
} else if (typeof x === "string" && typeof y === "string") {
return x + ' ' + y; // 字符串连接
}
throw new Error("Invalid arguments");
}