示例
/**
//不要为仅在某个位置上的参数类型不同的情况下定义重载
function fn(x: number) {
return "number类型"+x;
}
function fn(x: string) {
return "string类型"+x;
}
*/
//应该尽可能地使用联合类型
function fn(x: number|string) {
return "联合类型: "+x+"("+typeof(x)+")";
}
console.log(fn(5));
console.log(fn("ssss"));
运行测试