So now we got union types here.
Let's build upon that idea of union types and also introduce literal types literal types are types where
you don't just say that a certain variable or parameters should hold let's say a number or a string
but where you are very clear about the exact value it should hold.
We saw that early already here in basics concept number two for example.
Therefore hover over that you see the type which was inferred is not no it's two doors eight and types
did in third this concrete number as a type here because it's a constant.
It will never change in the effort hive knows the value I assign here is the exact type I have.
So of course this is a number you could say to dot eight is derived from the number type but it's a
specific number.
And this does not just exist for numbers but all four strings ends on and especially for strings it
can be very useful let's say here in our combined function we expect numbers or strings and we combine
them differently based on what we get but we also want to allow the caller off the function to define
how the results should be returned.
So that we can basically force a conversion from number to string or the other way around.
We could do this with a third parameter and we could name that let's say it result type or result conversion
or however you want to name it.
And I want to describe this with a string is all set as to type string.
No my idea is that I can call combine year with two numbers.
And for example pass in a string of as number and it's totally up to you which identifier you use here.
Down there we might have as text but then we might have another combination here
combined string ages where I pass in two strings but I still want to put this as number and this would
be a case where the third parameter can be helpful to force a conversion because if I pass in two strings
here normally we would make it into this ls branch and concatenated and I might want to return this
as a number instead so that I can all the console log combined string age is here.
So now we just have to take these identifiers into account in our function by including a result conversion
in our if checks.
Now the question is what exactly do we want to do.
Do we want to make sure dead we can force these two strings to be added as numbers or do we want to
force the result no matter if it was a mathematical calculation or a string concatenation to be converted
to a text or a number if we want to force a conversion we could ATF check here and check if it's salt
conversion is equal to as no and if that's the case I want to return resolved with a plus in front of
it which converts it to a number.
The alternative would be parse float for example but here I'll just go for the plus since this is a
bit shorter and else I want to return result to string to forests and output as a string.
So now we do the calculation based on the real type we get but then we convert the result that's one
way of doing that and if we save that and we then compile this file we see this is the result I get
and we get Dad output for example for our string combination here because as I said the combination
itself the calculation so to say is performed based on the type we feed in which here is a string.
So we concatenate it and then we just convert to result.
The alternative logic would be to check the type here and do a different operation based on the return
type or specifying here.
So we could comment this out and instead here say if the type of input 1 is number and type of input
2 is number or if result conversion is equal to AS number then I want to combine values like this and
now to a white as error here I forced the conversion to numbers by adding a plus in front of each input
so that each input is converted to a number and is guaranteed to be a number before I combined them.
Side note if you would pass in strings that can't be converted to a number so something like Max for
example.
Then you'll get an A n not a number as a result.
Well in the Dallas case so that we don't have as number or that we simply don't have no inputs.
Then we make it into this block here and now with that if I return result again here and we didn't safeties
and recompile we'll see a different output on our page.
Now we get 56 in these two lines here because now we're not converting the result.
So we're not converting that concatenated string for this line but instead we're doing the conversion
before we combine the two values.
So that's up to you which logic you want to implement.
I actually want to have a closer look at literal types concept which I described at the beginning of
this lecture I'm passing in as number and asked text and the downside of this is that right now we as
a developer have to memorize these values we have to make sure that we don't miss type here.
Now we could use that income to improve that but if we only have two values here as text or as number
then maybe alls and such a literal type could be an option we could say this shouldn't be any string.
It should be as number or as text.
So we use a union type combined with literal types literal types are the types which are based on your
core types.
String number and so on but you don't have a specific word in off that type.
So here we allow us specifically these two strings not any string just these two strings.
So we want a string for resolved conversion but it has to be one of these two values.
Any average string value will not be allowed.
So that's the idea behind a literal type.
And often you will use this in the context of a union type because you don't just want to allow one
exact value you could hardcoded into your code if that would be the case but you want to have two or
more possible values.
In this case we get two possible values.
So now we are guaranteed to get result conversion which is either as number or asked text for example
here if I now try to compare this to as number because I have a typo here typescript is able to tell
me here in the ITC and of course all if we save it and compiled here in the terminal because it knows
that as number without a r at the end will never be a valid value for a result conversion because I
set this union type with these literal types.
So now we again gain some extra type safety and ensure that we can't use this incorrectly the same of
course for passing in a value if we have a typo here we get an error only if we use ever as number or
s text we're allowed to compile this as you see if I now compiled is does works and we get the same
output as before year.
So these are literal types especially useful when used in conjunction with union types.