Periksa naskah apakah objek tipe
mySprite instanceof Sprite;
Dotch
mySprite instanceof Sprite;
function isFish(pet: Fish | Bird): pet is Fish {
return (<Fish>pet).swim !== undefined;
}
// Both calls to 'swim' and 'fly' are now okay.
if (isFish(pet)) {
pet.swim();
}
else {
pet.fly();
}