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