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

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)
}

posted on 2021-09-10 09:36  aisowe  阅读(112)  评论(0编辑  收藏  举报

导航