Okay so now we're going to talk about conditional typings. So 1
like we told you here, we don't want a string, we want 2
something that is either a frost spell name or a fire 3
spell name depending on the type that we received here, 4
okay? So if it's a fire spell, we want the fire spell name. 5
And the opposite as well. So basically what we want okay so 6
I'm going to write something that doesn't make sense at all 7
it's not like that that you're going to write it but that's 8
the idea basically we would like this type for example to be 9
conditionally something so something like this for example 10
if S extends fire spell so if basically this is a fire spell 11
then we want this type to be fire spell name otherwise we 12
want this type to be frost spell name and obviously you 13
cannot do it like this but you can create a type out of that 14
okay so when you want to write a condition like this you can 15
just take this create a type. So for example I could create a 16
type and not an interface and I will explain right after why 17
not an interface. So type that could be spell name 18
like this and so basically I want to say that if S extend 19
fairy spell then set fire spell name otherwise set frost 20
spell name but S doesn't exist here so basically here it's 21
like you're creating a function so you have to receive this S 22
as parameter okay this spell so here I'm going to receive the 23
spell like this and now I can use my spell name and say that 24
it's going to be of type of the spell that I've received here. 25
So this is basically this S is basically a fire spell or a 26
frost spell type. Okay and so I'm going to send this type 27
here and the condition is going to be applied and it will 28
return the correct type. So now if I go here 29
and try to send a fire spell name here. for example big bang 30
as you can see it's not working anymore because Fire spell big 31
bank is not assignable to frost spell name so Typescript 32
understood that since I have sent a frost spell here and 33
that my frost wizard is now of type frostpell it has sent this 34
frost spell as parameter right here so this is a frost spell 35
okay S is a frost spell so here it has called spell name frost 36
spell and here frost spell has been sent this S is a frost 37
spell and it does not extend via spell so this is what has 38
been returned okay so you can do that with types okay and 39
obviously this is an advanced topic right but you can do that 40
with type you could also do it with interfaces but that would 41
be less convenient because there is another difference 42
between types and interfaces with a type I can do that 43
example my string and say that is a string for example but I 44
cannot do that with an interface okay with interface 45
you always have to create an extra attribute okay I would 46
have to create for example name and then string so you have to 47
create another extra key why with the type you can directly 48
set a type without a key okay without creating an object okay 49
but I could do that here with an interface I would just add an 50
extra object here I would do something like this and here I 51
would receive my spell and this would be spell name okay so 52
this would work but here I would have to call for example 53
that spell name and in spell name I would have my. name 54
finally okay and so it wouldn't work anymore here because here 55
I would be forced to send a name here and finally my frost 56
spell name blizzard like this, okay? So it's less convenient 57
when you want to do a conditional type. All right? So 58
I will prefer to use type approach like this. 59
So don't worry this is a really advanced topic but I hope it's 60
clear and yeah we're good with that so congratulation and see 61
you in the next one.