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