Now a special type of type guard you could say or something that helps you with type guards is discriminated
union.
Now what's dead.
It's a pattern which you can use when working with union types that makes implementing type guards easier.
It is available when you work with object types.
Let me show you how it works let's say we have a couple of interfaces and this could be classes as well.
But here all go for interfaces to show that it works with those as well.
And there I have my Bert which has a flying speed which should be of type no let's say.
And I also have my horse which has a ground speed or a running speed which should be of type no so if
these two interfaces here now we create our own union type animal which is either a bird or a horse
and justice before we might have a function move.
Animal that takes an animal as an input which is of type animal.
So using our union type you now in their I want to say moving with speed and now of course I need two
axes either flying speed or running speed.
Now of course we could have just named that speed but this is just a simplified example of a real use
case you might face where you have some interfaces or objects in general that are kind of related but
have different properties or methods.
So here it's the speed property which actually is different.
So I can't ex is flying speed here because of course not all animals have a flying speed.
Now we can do what we learned before we can check if flying speed is in animal here and if that is the
case we can execute this code now this is an approach we can take but the more potential animals we
get there the more things we have to check.
We also can mis type here in this string.
We can forget an E for example and wonder why it is won't work.
Now we can't use instance off here because I'm breaking with interfaces as I explained.
Animal instance of bird will not work because data is an interface which is not compiled to javascript.
So at runtime this will not be available as a constructor function.
So the effort is does not work.
Now we can build a discriminated union here by giving every interface so every object which should be
part of that union an extra property you can use any name you want of you use kind or type here.
I'll go for type and name this bird now important we're trading an interface here.
So this is actually not a value for the type property.
This instead is a literal type.
As you learned where type must hold a string which must be bird.
So this is a type assignment where we narrowed down the value that may be stored in type to exactly
this string value and I do the same for horse just with horse as a literal type.
Now here in our function we can use a switch statement and switch on animal dots type every animal has
a type property because we added it to all interfaces that make up our animal.
And then here we can have a couple of cases where we even get auto completion because TypeScript and
our I.T. understands that type can only ever be bird or horse and if it's bird then we could set some
internal variable speed to let's say animal dot flying speed and then brake thereafter otherwise if
the case is horse we said speed equal to animal not running speed and there after we can console log
moving at speed plus speed and if I now call move animal with an animal I create on the fly here where
I set to type two bird and I'm only allowed to use bird or horse then I have to set my flying speed
and by the way types could even recognizes that a concept running speed here to try to do that and set
us to 10 it complains that this is not a way lable for type bird.
If I said two flying speed however it works and if we save that it outputs this correctly and also compiles
without errors.
Now this is a discriminated union because we have one common property in every object that makes up
our union which describes Stat object so that we can use this property that describes this object in
our check to have 100 percent type safety and understand which properties are available for such an
object and which properties are not.
So this is a no useful pattern which you can use when working with objects and with union types and
it even works with interfaces because this interface forces every object you built based on this interface
to have this type so instead of checking for the existence of a given property or instead of using instance
of where you as a property we note it exists to check which type of object we're working with we also
eliminate the danger of mis typing because typescript understands that the only cases we can have here
for animal type are bird and horse and hence it gives us does auto completion and if we would introduce
a typo we would immediately get an error.
So therefore it is a very useful pattern when working with objects and union types.