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 trainAnimal(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' || typeof second === 'string') { return `${first}${second}` } return first + second}// 使用 instanceof 语法来做类型保护class NumberObj { count: number}function add(first: object | NumberObj, second: object | NumberObj) { if (first instanceof NumberObj && second instanceof NumberObj) { return first.count + second.count } return 0}