1 00:00:02,360 --> 00:00:04,140 So now we got union types here. 2 00:00:04,500 --> 00:00:13,110 Let's build upon that idea of union types and also introduce literal types literal types are types where 3 00:00:13,110 --> 00:00:19,260 you don't just say that a certain variable or parameters should hold let's say a number or a string 4 00:00:19,680 --> 00:00:23,850 but where you are very clear about the exact value it should hold. 5 00:00:23,850 --> 00:00:29,250 We saw that early already here in basics concept number two for example. 6 00:00:29,260 --> 00:00:36,360 Therefore hover over that you see the type which was inferred is not no it's two doors eight and types 7 00:00:36,490 --> 00:00:41,210 did in third this concrete number as a type here because it's a constant. 8 00:00:41,250 --> 00:00:47,960 It will never change in the effort hive knows the value I assign here is the exact type I have. 9 00:00:47,970 --> 00:00:54,540 So of course this is a number you could say to dot eight is derived from the number type but it's a 10 00:00:54,540 --> 00:00:55,970 specific number. 11 00:00:56,100 --> 00:01:01,380 And this does not just exist for numbers but all four strings ends on and especially for strings it 12 00:01:01,380 --> 00:01:08,880 can be very useful let's say here in our combined function we expect numbers or strings and we combine 13 00:01:08,880 --> 00:01:14,970 them differently based on what we get but we also want to allow the caller off the function to define 14 00:01:15,570 --> 00:01:18,180 how the results should be returned. 15 00:01:18,180 --> 00:01:24,000 So that we can basically force a conversion from number to string or the other way around. 16 00:01:24,120 --> 00:01:31,170 We could do this with a third parameter and we could name that let's say it result type or result conversion 17 00:01:31,180 --> 00:01:33,180 or however you want to name it. 18 00:01:33,180 --> 00:01:38,710 And I want to describe this with a string is all set as to type string. 19 00:01:38,730 --> 00:01:43,110 No my idea is that I can call combine year with two numbers. 20 00:01:43,110 --> 00:01:52,070 And for example pass in a string of as number and it's totally up to you which identifier you use here. 21 00:01:52,200 --> 00:01:59,220 Down there we might have as text but then we might have another combination here 22 00:02:02,870 --> 00:02:10,040 combined string ages where I pass in two strings but I still want to put this as number and this would 23 00:02:10,040 --> 00:02:16,610 be a case where the third parameter can be helpful to force a conversion because if I pass in two strings 24 00:02:16,610 --> 00:02:23,900 here normally we would make it into this ls branch and concatenated and I might want to return this 25 00:02:23,990 --> 00:02:31,390 as a number instead so that I can all the console log combined string age is here. 26 00:02:31,460 --> 00:02:37,880 So now we just have to take these identifiers into account in our function by including a result conversion 27 00:02:37,880 --> 00:02:40,170 in our if checks. 28 00:02:40,190 --> 00:02:42,760 Now the question is what exactly do we want to do. 29 00:02:42,800 --> 00:02:50,030 Do we want to make sure dead we can force these two strings to be added as numbers or do we want to 30 00:02:50,750 --> 00:02:58,160 force the result no matter if it was a mathematical calculation or a string concatenation to be converted 31 00:02:58,460 --> 00:03:05,960 to a text or a number if we want to force a conversion we could ATF check here and check if it's salt 32 00:03:05,960 --> 00:03:14,930 conversion is equal to as no and if that's the case I want to return resolved with a plus in front of 33 00:03:14,930 --> 00:03:17,190 it which converts it to a number. 34 00:03:17,210 --> 00:03:25,080 The alternative would be parse float for example but here I'll just go for the plus since this is a 35 00:03:25,080 --> 00:03:34,530 bit shorter and else I want to return result to string to forests and output as a string. 36 00:03:34,610 --> 00:03:41,660 So now we do the calculation based on the real type we get but then we convert the result that's one 37 00:03:41,660 --> 00:03:48,700 way of doing that and if we save that and we then compile this file we see this is the result I get 38 00:03:48,730 --> 00:03:56,350 and we get Dad output for example for our string combination here because as I said the combination 39 00:03:56,350 --> 00:04:02,550 itself the calculation so to say is performed based on the type we feed in which here is a string. 40 00:04:02,560 --> 00:04:05,880 So we concatenate it and then we just convert to result. 41 00:04:05,920 --> 00:04:13,900 The alternative logic would be to check the type here and do a different operation based on the return 42 00:04:13,900 --> 00:04:15,680 type or specifying here. 43 00:04:15,910 --> 00:04:22,960 So we could comment this out and instead here say if the type of input 1 is number and type of input 44 00:04:22,960 --> 00:04:35,980 2 is number or if result conversion is equal to AS number then I want to combine values like this and 45 00:04:35,980 --> 00:04:42,760 now to a white as error here I forced the conversion to numbers by adding a plus in front of each input 46 00:04:43,030 --> 00:04:48,550 so that each input is converted to a number and is guaranteed to be a number before I combined them. 47 00:04:48,550 --> 00:04:53,800 Side note if you would pass in strings that can't be converted to a number so something like Max for 48 00:04:53,800 --> 00:04:54,640 example. 49 00:04:54,640 --> 00:04:58,760 Then you'll get an A n not a number as a result. 50 00:04:58,840 --> 00:05:04,680 Well in the Dallas case so that we don't have as number or that we simply don't have no inputs. 51 00:05:04,840 --> 00:05:14,620 Then we make it into this block here and now with that if I return result again here and we didn't safeties 52 00:05:14,620 --> 00:05:18,460 and recompile we'll see a different output on our page. 53 00:05:18,460 --> 00:05:23,410 Now we get 56 in these two lines here because now we're not converting the result. 54 00:05:23,530 --> 00:05:28,570 So we're not converting that concatenated string for this line but instead we're doing the conversion 55 00:05:28,720 --> 00:05:30,850 before we combine the two values. 56 00:05:30,940 --> 00:05:33,640 So that's up to you which logic you want to implement. 57 00:05:33,640 --> 00:05:38,620 I actually want to have a closer look at literal types concept which I described at the beginning of 58 00:05:38,620 --> 00:05:45,040 this lecture I'm passing in as number and asked text and the downside of this is that right now we as 59 00:05:45,040 --> 00:05:50,480 a developer have to memorize these values we have to make sure that we don't miss type here. 60 00:05:50,500 --> 00:05:57,280 Now we could use that income to improve that but if we only have two values here as text or as number 61 00:05:57,550 --> 00:06:03,450 then maybe alls and such a literal type could be an option we could say this shouldn't be any string. 62 00:06:03,670 --> 00:06:09,940 It should be as number or as text. 63 00:06:09,940 --> 00:06:16,870 So we use a union type combined with literal types literal types are the types which are based on your 64 00:06:16,870 --> 00:06:17,710 core types. 65 00:06:17,710 --> 00:06:22,450 String number and so on but you don't have a specific word in off that type. 66 00:06:22,450 --> 00:06:28,000 So here we allow us specifically these two strings not any string just these two strings. 67 00:06:28,000 --> 00:06:33,170 So we want a string for resolved conversion but it has to be one of these two values. 68 00:06:33,310 --> 00:06:35,910 Any average string value will not be allowed. 69 00:06:35,920 --> 00:06:38,300 So that's the idea behind a literal type. 70 00:06:38,380 --> 00:06:43,090 And often you will use this in the context of a union type because you don't just want to allow one 71 00:06:43,090 --> 00:06:48,460 exact value you could hardcoded into your code if that would be the case but you want to have two or 72 00:06:48,460 --> 00:06:50,170 more possible values. 73 00:06:50,170 --> 00:06:52,470 In this case we get two possible values. 74 00:06:52,630 --> 00:07:00,250 So now we are guaranteed to get result conversion which is either as number or asked text for example 75 00:07:00,250 --> 00:07:06,280 here if I now try to compare this to as number because I have a typo here typescript is able to tell 76 00:07:06,280 --> 00:07:13,660 me here in the ITC and of course all if we save it and compiled here in the terminal because it knows 77 00:07:13,930 --> 00:07:21,110 that as number without a r at the end will never be a valid value for a result conversion because I 78 00:07:21,110 --> 00:07:25,550 set this union type with these literal types. 79 00:07:25,580 --> 00:07:32,120 So now we again gain some extra type safety and ensure that we can't use this incorrectly the same of 80 00:07:32,120 --> 00:07:38,840 course for passing in a value if we have a typo here we get an error only if we use ever as number or 81 00:07:38,840 --> 00:07:45,200 s text we're allowed to compile this as you see if I now compiled is does works and we get the same 82 00:07:45,200 --> 00:07:46,780 output as before year. 83 00:07:46,820 --> 00:07:52,040 So these are literal types especially useful when used in conjunction with union types.