📜  检查对象类型打字稿代码示例

📅  最后修改于: 2022-03-11 14:48:17.449000             🧑  作者: Mango

代码示例1
function isFish(pet: Fish | Bird): pet is Fish {
   return (pet).swim !== undefined;
}

// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
  pet.swim();
}
else {
  pet.fly();
}