Okay so now let's work on the wizard class. So let's create a 1
new class that is going to be wizard. So a wizard is a class 2
associated to a spell either fire spell or frost spell so 3
the first thing we may think that we can do is something 4
like this here a wizard is going to be associated to a 5
spell okay we come back to that later because you will see that 6
there is a better way than doing that then we should have 7
a spell book right so I'm going to have a spell book that 8
should be an array and it should be a spell array, right? 9
Then we have to initialize the spell book. So I'm just going 10
to create a constructor. Here I'm going to receive a spell 11
book that should have the exact same type here and I'm going to 12
fill it. Boom. But then we have a function to cast all the 13
spells all at once. So I'm going to call cast all at once. 14
And basically it's going to loop over the spell book. And 15
for each of them so this is a spell that should have a type 16
spell we're just going to do a spell that but hm and as you 17
can see I don't have th auto completion and there is a good 18
reason for that it's because Typescript doesn't know that 19
it's really a spell okay I could have called that I don't 20
know S like this okay so for now Typescript doesn't know at 21
all what this is so that's an issue so how to do that how to 22
make a Typescript understand that this is something that 23
should be a spell but it can be anything it can be a fire spell 24
or a fraud spell when you can actually write here that S is 25
something that extends a spell and now this is a real type 26
here so this is anything that extends a spell and now here as 27
you can see I have a name and I have the cast function. So 28
that's how you can do it, okay? And obviously you couldn't know 29
how to do that, okay? But now you're going to remember. So 30
you can write things like this in here and you're going to see 31
that we will be able to write even more complex things in 32
generate types. So anyway, now the the type S here refer to 33
something that extends a spell. So we can create the cast from 34
spell book. And so here we're supposed to receive a name okay 35
so for now it's going to be a string but later you will see 36
that we will be able to actually change this type so 37
cast spell from book yes so what we're going to do is we're 38
going to try to look in the spell book if this spell exists 39
so I'm going to do a spell and I'm going to go and look in my 40
spell book if I can find 41
this spell and to know if I can find it I'm going to look at 42
the spell name so if the spell name is equal to this name then 43
we will have a spell so if there is a spell then we're 44
going to cast it and if we don't we're going to throw a 45
new error and we're going to say that you don't have this 46
spell in your spell book. Like this. Okay not bad not bad we 47
are almost done technically now we should be able to for 48
example create a wizard that is a new wizard and here we should 49
be able to send an array of spells so we're just going to 50
create fire spells that should be of type fire spells array 51
like this and I'm going to create an array like this with 52
a new fire spell inside and I'm going to create a name but I 53
can directly use my fire spell name enem here fire spell name 54
and I can create for example a fire bolt like this so I have 55
an array of one fire spell and technically I can send this to 56
my wizard and now my wizard is a wizard of type fire spell and 57
I should be able to force it okay I should able to force it 58
by typing fire spell like this and now I cannot send something 59
else than a a fire spell. So if I create for example an array 60
of frost spells like this frost spells frost spells and here 61
I'm going to use the frost spell name dot blizzard for 62
example and here it's going to be a frost spell technically I 63
shouldn't be able to send something like this and as you 64
can see it's not working argument of type frost spell 65
array is not assignable to parameter fire spell array 66
because of that okay but if I remove it now it's alright okay 67
cool so now we are almost good because there is one last thing 68
that I don't really like and it's it's it's it's this okay 69
because I have set a string so technically I can send anything 70
let me give you an example if I use cast spell from book on my 71
wizard so wizard. cast spell from book here I can send a 72
string okay but what I really would like is that if my wizard 73
is of type frost spell I would like to be able to undescend a 74
frost spell name. And if my wizard is of type fire spell, I 75
would like to be able to undescend a fire spell name. 76
Okay? So since this is really more advanced, we're going to 77
do that at the very end of the project, okay? For now we're 78
going to leave that like this. Okay? So now we're going to 79
assume that for now we are good, okay? We are able to 80
create a wizard that has a very specific type and we are able 81
to create et cetera. All the allergic seems to work so we're 82
going to work on the story in the next video. See you in the 83
next one.