typescript (type guard)

Instead of using instanceof, or checking for an existing property, we create a discriminating union type.

Type Guard is the idea that a certain property exists

Type Guarding for types

Much better way is to use instanceof when trying to figure out what type the instance is. Because literally, instanceof is implemented to check if this particular instance is a type of something.

Discriminated Union

…helps with type guard.

3) discriminated Union types
can add type: ‘bird’, ‘horse’, then use switch animal.type in moveAnimal