访问联合类型中某个类型特有的属性或方法
index.ts
interface Cat {
name: string
run(): void
}
interface Fish {
name: string
swim(): void
}
function isCat(animal: Cat | Fish): boolean {
return Boolean((animal as Cat).run)
}
index.ts
interface Cat {
name: string
run(): void
}
interface Fish {
name: string
swim(): void
}
function isCat(animal: Cat | Fish): boolean {
return Boolean((animal as Cat).run)
}