interface Bird {
fly: boolean;
sing: () => {};
}
interface Dog {
fly: boolean;
bark: () => {};
}
// 类型断言的方式
function trainAnimal(animal: Bird | Dog){
if(animal.fly){
(animal as Bird).sing()
}else {
(animal as Dog).bark()
}
}
// in 语法来做类型保护
function trainAnimalSecond(animal: Bird | Dog){
if('sing' in animal){
animal.sing()
}else {
animal.bark()
}
}
// typeof 语法来做类型保护
function add(first: string | number, second: string | number){
if(typeof first === 'string' || second === 'string'){
return `${first}${second}`
}else {
return first + second
}
}
// 使用instanceof 语法来做类型保护
class NumberObj{
count: number
}
function addSecond (first : object | NumberObj, second: object | NumberObj){
if(first instanceof NumberObj && second instanceof NumberObj){
return first.count + second.count
}
return 0
}