【ts】ts学习笔记
一、函数重载
// 函数重载
function add(...rest: number[]):number;
function add(...rest: string[]):string;
function add(...rest: any):any {
let first = rest[0];
if(typeof first === 'number') {
return rest.reduce((pre, cur) => pre +cur);
}
if(typeof first === 'string') {
return rest.join('');
}
}
//调用
const b = add(1,2,3,4);
console.log(b);
还没有评论,来说两句吧...