interface Waiter{
anjiao:boolean;
say:()=>{}
}
interface Teacher{
anjiao:boolean;
skill:()=>{};
}
//只要参数有或者两种以上,就叫做联合类型
function judgeWho(animal:Waiter | Teacher){
//他不知道这个方法是在Waiter里还是Teacher
// animal.say()
//类型保护,断言, true 还是 false
if(animal.anjiao){
(animal as Teacher).skill();
}else{
(animal as Waiter).say();
}
}
function judgeWhoTow(animal:Waiter | Teacher){
if('skill' in animal){
animal.skill()
}else{
animal.say()
}
}
function add(first : string | number ,second:string | number){
if(typeof first === "string" || typeof second === "string"){
return `${first}${second}`;
}
return first + second;
}
class NumberObj{
count : number;
}
function addObj(first : object | NumberObj , second :object | NumberObj){
if(first instanceof NumberObj && second instanceof NumberObj){
return first.count + second.count
}
return 0;
}