联合类型和类型守护(类型守护)
interface Waiter {
a:boolean;
say: ()=>{};
}
interface Teacher {
a:boolean;
skill: ()=>{};
}
function judgeWho(animal:Waiter | Teacher) {
if(animal.a) {
(animal as Teacher).skill();
} else {
(animal as Waiter).say();
}
}
function judgeWhoTwo(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 intanceof NumberObj && second instanceof NumberObj) {
return first.count + second.count;
}
return 0;
}