1 00:00:02,240 --> 00:00:09,240 Now a special type of type guard you could say or something that helps you with type guards is discriminated 2 00:00:09,240 --> 00:00:09,890 union. 3 00:00:09,950 --> 00:00:11,310 Now what's dead. 4 00:00:11,390 --> 00:00:18,980 It's a pattern which you can use when working with union types that makes implementing type guards easier. 5 00:00:18,980 --> 00:00:22,020 It is available when you work with object types. 6 00:00:22,040 --> 00:00:28,480 Let me show you how it works let's say we have a couple of interfaces and this could be classes as well. 7 00:00:28,490 --> 00:00:32,000 But here all go for interfaces to show that it works with those as well. 8 00:00:33,230 --> 00:00:42,520 And there I have my Bert which has a flying speed which should be of type no let's say. 9 00:00:42,720 --> 00:00:53,410 And I also have my horse which has a ground speed or a running speed which should be of type no so if 10 00:00:53,420 --> 00:01:02,920 these two interfaces here now we create our own union type animal which is either a bird or a horse 11 00:01:03,430 --> 00:01:07,780 and justice before we might have a function move. 12 00:01:07,870 --> 00:01:13,040 Animal that takes an animal as an input which is of type animal. 13 00:01:13,070 --> 00:01:21,630 So using our union type you now in their I want to say moving with speed and now of course I need two 14 00:01:21,630 --> 00:01:25,230 axes either flying speed or running speed. 15 00:01:25,230 --> 00:01:30,600 Now of course we could have just named that speed but this is just a simplified example of a real use 16 00:01:30,600 --> 00:01:37,590 case you might face where you have some interfaces or objects in general that are kind of related but 17 00:01:37,590 --> 00:01:40,530 have different properties or methods. 18 00:01:40,530 --> 00:01:43,640 So here it's the speed property which actually is different. 19 00:01:43,950 --> 00:01:49,230 So I can't ex is flying speed here because of course not all animals have a flying speed. 20 00:01:49,260 --> 00:01:58,850 Now we can do what we learned before we can check if flying speed is in animal here and if that is the 21 00:01:58,850 --> 00:02:06,260 case we can execute this code now this is an approach we can take but the more potential animals we 22 00:02:06,260 --> 00:02:10,900 get there the more things we have to check. 23 00:02:10,920 --> 00:02:14,130 We also can mis type here in this string. 24 00:02:14,130 --> 00:02:18,480 We can forget an E for example and wonder why it is won't work. 25 00:02:18,480 --> 00:02:23,780 Now we can't use instance off here because I'm breaking with interfaces as I explained. 26 00:02:23,880 --> 00:02:31,850 Animal instance of bird will not work because data is an interface which is not compiled to javascript. 27 00:02:31,860 --> 00:02:36,270 So at runtime this will not be available as a constructor function. 28 00:02:36,270 --> 00:02:37,740 So the effort is does not work. 29 00:02:37,740 --> 00:02:44,070 Now we can build a discriminated union here by giving every interface so every object which should be 30 00:02:44,070 --> 00:02:51,490 part of that union an extra property you can use any name you want of you use kind or type here. 31 00:02:51,560 --> 00:02:57,500 I'll go for type and name this bird now important we're trading an interface here. 32 00:02:57,510 --> 00:03:02,640 So this is actually not a value for the type property. 33 00:03:02,640 --> 00:03:04,580 This instead is a literal type. 34 00:03:04,580 --> 00:03:09,140 As you learned where type must hold a string which must be bird. 35 00:03:09,150 --> 00:03:15,950 So this is a type assignment where we narrowed down the value that may be stored in type to exactly 36 00:03:15,960 --> 00:03:23,430 this string value and I do the same for horse just with horse as a literal type. 37 00:03:23,430 --> 00:03:33,180 Now here in our function we can use a switch statement and switch on animal dots type every animal has 38 00:03:33,180 --> 00:03:38,350 a type property because we added it to all interfaces that make up our animal. 39 00:03:38,550 --> 00:03:43,560 And then here we can have a couple of cases where we even get auto completion because TypeScript and 40 00:03:43,560 --> 00:03:51,450 our I.T. understands that type can only ever be bird or horse and if it's bird then we could set some 41 00:03:51,660 --> 00:04:03,720 internal variable speed to let's say animal dot flying speed and then brake thereafter otherwise if 42 00:04:03,720 --> 00:04:16,530 the case is horse we said speed equal to animal not running speed and there after we can console log 43 00:04:17,520 --> 00:04:28,220 moving at speed plus speed and if I now call move animal with an animal I create on the fly here where 44 00:04:28,220 --> 00:04:36,950 I set to type two bird and I'm only allowed to use bird or horse then I have to set my flying speed 45 00:04:36,950 --> 00:04:43,400 and by the way types could even recognizes that a concept running speed here to try to do that and set 46 00:04:43,400 --> 00:04:49,040 us to 10 it complains that this is not a way lable for type bird. 47 00:04:49,040 --> 00:04:56,300 If I said two flying speed however it works and if we save that it outputs this correctly and also compiles 48 00:04:56,300 --> 00:04:57,740 without errors. 49 00:04:57,740 --> 00:05:04,820 Now this is a discriminated union because we have one common property in every object that makes up 50 00:05:04,830 --> 00:05:11,750 our union which describes Stat object so that we can use this property that describes this object in 51 00:05:11,750 --> 00:05:19,220 our check to have 100 percent type safety and understand which properties are available for such an 52 00:05:19,220 --> 00:05:21,970 object and which properties are not. 53 00:05:21,980 --> 00:05:28,430 So this is a no useful pattern which you can use when working with objects and with union types and 54 00:05:28,430 --> 00:05:35,120 it even works with interfaces because this interface forces every object you built based on this interface 55 00:05:35,300 --> 00:05:42,950 to have this type so instead of checking for the existence of a given property or instead of using instance 56 00:05:42,950 --> 00:05:49,760 of where you as a property we note it exists to check which type of object we're working with we also 57 00:05:49,760 --> 00:05:55,670 eliminate the danger of mis typing because typescript understands that the only cases we can have here 58 00:05:55,910 --> 00:06:02,060 for animal type are bird and horse and hence it gives us does auto completion and if we would introduce 59 00:06:02,060 --> 00:06:04,560 a typo we would immediately get an error. 60 00:06:04,730 --> 00:06:09,200 So therefore it is a very useful pattern when working with objects and union types.