访问联合类型中某个类型特有的属性或方法

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)
}
原文地址:https://www.cnblogs.com/aisowe/p/15250009.html