Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,420 --> 00:00:05,250
So we have created our state and it is working the way that we expect it to.
2
00:00:05,610 --> 00:00:11,640
However, right now we have two potential problems that could arise, especially if we haven't worked
3
00:00:11,640 --> 00:00:16,050
on this application for a while or if we're working with a larger team.
4
00:00:16,440 --> 00:00:18,960
So let's actually talk about that issue.
5
00:00:19,440 --> 00:00:27,570
Right now, our options is an object, and that object should only include the gender popularity and
6
00:00:27,570 --> 00:00:30,840
the length because those are the only options that we have.
7
00:00:31,260 --> 00:00:36,990
However, right now we have the ability to add other properties.
8
00:00:37,230 --> 00:00:43,150
So for example, we can add this random property called milk and we can assign that to be equal to true.
9
00:00:43,470 --> 00:00:49,920
And this is a okay, the way that we have set things up and obviously we probably do not want to add
10
00:00:49,920 --> 00:00:53,250
this property inside of this reactive state.
11
00:00:53,610 --> 00:00:55,260
So that's problem number one.
12
00:00:55,650 --> 00:00:59,700
Problem number two is the value of these properties.
13
00:01:00,090 --> 00:01:05,760
Right now, we have specified that this is a girl, but it also could be a boy and it also could be
14
00:01:05,760 --> 00:01:06,630
unisex.
15
00:01:06,930 --> 00:01:13,770
However, we could also specify that this is going to be something like know girly or whatever.
16
00:01:13,770 --> 00:01:15,780
We can add whatever thing that we want.
17
00:01:15,900 --> 00:01:18,930
And obviously we do not want to have this ability.
18
00:01:19,200 --> 00:01:22,200
You can either choose girl, boy or unisex.
19
00:01:22,320 --> 00:01:24,450
So I'm really over here for this property.
20
00:01:24,660 --> 00:01:27,500
You can only choose unique or trendy.
21
00:01:27,510 --> 00:01:29,400
You can't put anything else in there.
22
00:01:29,760 --> 00:01:35,880
So those are the two potential issues that we are facing now in order to fix this.
23
00:01:36,120 --> 00:01:44,310
What we need to do is explicitly define the type of our properties and this is where TypeScript comes
24
00:01:44,310 --> 00:01:45,510
in to place.
25
00:01:45,780 --> 00:01:49,470
So right now we've been using plain old vanilla JavaScript.
26
00:01:49,770 --> 00:01:56,040
However, what we want to do is use TypeScript to define the types of our variables and our state.
27
00:01:56,280 --> 00:01:59,430
Now, if this doesn't make much sense to you, well, let me explain.
28
00:01:59,910 --> 00:02:06,630
So in order to actually utilize TypeScript inside of this script tag, all we need to do is right after
29
00:02:06,630 --> 00:02:07,830
this set up or before.
30
00:02:07,830 --> 00:02:14,520
If you want to, you can specify that the language is going to be equal to T.S. and we can go ahead
31
00:02:14,520 --> 00:02:15,390
and save that.
32
00:02:15,390 --> 00:02:16,100
And there we go.
33
00:02:16,110 --> 00:02:16,620
Now we are.
34
00:02:16,950 --> 00:02:18,510
Now we can utilize TypeScript.
35
00:02:18,870 --> 00:02:23,880
Now, right away you can see that nothing has really changed if you go back to our application and refresh.
36
00:02:24,150 --> 00:02:26,390
Nothing has changed and that's A-OK.
37
00:02:26,430 --> 00:02:27,540
This is expected.
38
00:02:27,780 --> 00:02:32,280
But now what we're going to do is get a bunch of different properties that we can utilize to define
39
00:02:32,340 --> 00:02:33,690
specific types.
40
00:02:33,990 --> 00:02:38,460
Just to quickly explain exactly what I mean here, I'm going to declare a variable.
41
00:02:38,460 --> 00:02:40,080
I'm not going to make this a let.
42
00:02:40,410 --> 00:02:43,830
I'm going to say let my name.
43
00:02:44,070 --> 00:02:47,010
I'm going to say that this is going to be equal to length like.
44
00:02:47,010 --> 00:02:55,440
So now over here, I'm going to go ahead and reassign my name to my age, which is 25.
45
00:02:55,890 --> 00:02:59,490
Now, if we weren't using TypeScript, let me just get rid of this for now.
46
00:02:59,760 --> 00:03:01,350
You can see and save that.
47
00:03:01,350 --> 00:03:04,050
You can see that this is actually a.
48
00:03:04,080 --> 00:03:04,650
Okay.
49
00:03:04,650 --> 00:03:06,180
This is completely fine.
50
00:03:06,600 --> 00:03:10,670
Now, if we go back and we add TypeScript again, now this is actually not fine.
51
00:03:10,680 --> 00:03:12,510
We get this squiggly line.
52
00:03:12,750 --> 00:03:18,930
And if you go over here, you can see that it's saying that the type number is not assignable to type
53
00:03:18,930 --> 00:03:19,500
string.
54
00:03:19,890 --> 00:03:26,520
So because what we did here was we declared that this is going to be equal to length, the string length.
55
00:03:26,910 --> 00:03:30,900
It gave this the type of string and that's basically how it assigned it.
56
00:03:30,900 --> 00:03:33,690
So it is like let my name colon string.
57
00:03:33,910 --> 00:03:38,720
And we can also explicitly say that this is going to be of type string like so.
58
00:03:39,230 --> 00:03:45,030
Now what we're doing is we're trying to reassign that to a number and TypeScript is yelling at us,
59
00:03:45,210 --> 00:03:46,650
so we can't do this.
60
00:03:46,860 --> 00:03:53,100
And this is really good because of course we don't want to we don't often want to change types.
61
00:03:53,880 --> 00:03:55,410
So over here we can do this.
62
00:03:55,710 --> 00:04:01,470
And if we said something like 23, then now it's going to yell out of saying that this is equal to 23
63
00:04:01,470 --> 00:04:02,700
when it should be a string.
64
00:04:03,030 --> 00:04:08,910
So over here that is going to give us a little bit of extra security and maintain the integrity of our
65
00:04:08,910 --> 00:04:09,600
code base.
66
00:04:10,080 --> 00:04:16,260
So now what we're going to do is we're actually going to define the type of this state object.
67
00:04:16,620 --> 00:04:19,770
So just for now, I'm going to just duplicate this object.
68
00:04:19,770 --> 00:04:24,960
I'm say content object, and I'm going to say that this is going to be equal to an empty object.
69
00:04:25,190 --> 00:04:29,550
I'm going to try to give it the exact same type as we have over here.
70
00:04:30,150 --> 00:04:37,140
Now, in order to define a type of an object, TypeScript gives us this very important thing called
71
00:04:37,140 --> 00:04:37,950
interface.
72
00:04:38,220 --> 00:04:41,550
So this is going to allow us to define the type of an object.
73
00:04:41,940 --> 00:04:46,470
So we're going to say interface and I'm going to call this, well, whatever it is that we want to call
74
00:04:46,470 --> 00:04:46,590
it.
75
00:04:46,590 --> 00:04:50,820
So I'm going to call this option state because that's what we're trying to define.
76
00:04:50,820 --> 00:04:51,810
The option state.
77
00:04:52,080 --> 00:04:53,970
I will do curly braces.
78
00:04:54,300 --> 00:04:58,740
Now, right in here, we're going to define the type of each key value pair.
79
00:04:59,130 --> 00:04:59,570
So the.
80
00:04:59,610 --> 00:05:03,750
First thing we're going to do is we're going to say that, hey, there is going to have a key and that's
81
00:05:03,750 --> 00:05:05,220
going to be called gender.
82
00:05:05,490 --> 00:05:08,250
And this is going to have a type of string.
83
00:05:08,670 --> 00:05:10,050
And then we do semicolons.
84
00:05:10,290 --> 00:05:16,680
We're also going to say that this object should have a popularity key and it should have a value of
85
00:05:16,680 --> 00:05:17,310
string.
86
00:05:17,610 --> 00:05:23,550
And lastly, we should have a length key that has also a value of string.
87
00:05:23,970 --> 00:05:31,110
And now what we can do is we can actually apply this interface to this object so we can say colon and
88
00:05:31,110 --> 00:05:32,820
then options state.
89
00:05:33,030 --> 00:05:37,320
And right away, you can see that we're getting a little bit of issues telling us, hey, we do not
90
00:05:37,320 --> 00:05:42,540
have the gender or popularity or length keys right over here.
91
00:05:42,990 --> 00:05:44,670
So now what we can do is we can apply gender.
92
00:05:44,670 --> 00:05:47,910
And you can see right away we're actually getting this cool autofill.
93
00:05:48,310 --> 00:05:50,010
That's because it understands the type.
94
00:05:50,010 --> 00:05:51,090
So we can say gender.
95
00:05:51,450 --> 00:05:53,850
You just put whatever string that we want in here.
96
00:05:54,570 --> 00:05:55,800
Let's just say girl for now.
97
00:05:56,130 --> 00:06:03,060
And then over here we can say something like length and we can say all.
98
00:06:03,510 --> 00:06:08,340
And then lastly, the popularity is going to be unique.
99
00:06:08,340 --> 00:06:09,150
Like so.
100
00:06:09,720 --> 00:06:12,630
So right away you can see that now it's happy with us.
101
00:06:12,630 --> 00:06:17,910
And if we added in additional property like milk is equal to true, it's going to yell at us because
102
00:06:17,910 --> 00:06:21,620
it's saying that, hey, this isn't part of the type right over here.
103
00:06:21,630 --> 00:06:22,650
We didn't define this.
104
00:06:23,040 --> 00:06:28,380
So if this was indeed a property that we wanted to add, we would have to explicitly go to the interface
105
00:06:28,560 --> 00:06:33,210
and then define here that we want to have a property called Milk that is a boolean.
106
00:06:33,390 --> 00:06:36,780
And that pretty much means that yes, we indeed want to do this.
107
00:06:36,810 --> 00:06:38,100
Now, of course, we don't want to do this.
108
00:06:38,430 --> 00:06:40,080
So this is go ahead and get rid of that.
109
00:06:40,680 --> 00:06:43,130
So that solves problem number one.
110
00:06:43,140 --> 00:06:44,710
But what is problem number two?
111
00:06:44,730 --> 00:06:49,950
Well, problem number two is we're defining that this is going to be a very generic string.
112
00:06:50,310 --> 00:06:55,830
So we could, if you wanted to over here, instead of saying maybe length of all we can say length of
113
00:06:55,830 --> 00:06:56,520
medium.
114
00:06:56,790 --> 00:06:59,960
And that's actually not an option that we support.
115
00:06:59,970 --> 00:07:06,480
We only have short, long or all of them, but we don't have this medium option.
116
00:07:06,750 --> 00:07:11,040
Now, maybe another engineer that's not very familiar with this up, they could add this in there,
117
00:07:11,040 --> 00:07:13,140
and that's completely fine with TypeScript.
118
00:07:13,680 --> 00:07:20,190
So what we could do is we need to actually explicitly say the different types of strings that we can
119
00:07:20,190 --> 00:07:21,900
have for a specific key.
120
00:07:22,620 --> 00:07:27,750
Now in TypeScript, there's another thing that we can utilize called an enum to define this.
121
00:07:28,140 --> 00:07:31,560
So we're going to say enum gender array to do curly braces.
122
00:07:31,830 --> 00:07:37,470
And then right in here, we're going to define all of the different options that we have for a particular
123
00:07:37,470 --> 00:07:38,550
gender option.
124
00:07:38,910 --> 00:07:43,620
So over here I'm going to say gender and I'm going to say something like, girl.
125
00:07:43,770 --> 00:07:45,450
And this is all going to be in capital.
126
00:07:45,690 --> 00:07:50,880
We can actually assign that to the value of the string value of girl like.
127
00:07:50,880 --> 00:07:54,060
So the number do comma as all capital.
128
00:07:54,060 --> 00:08:00,390
Just because there's just a common convention where I say, boy, and then over here we going to say
129
00:08:00,390 --> 00:08:01,530
unisex.
130
00:08:01,980 --> 00:08:05,490
And that's going to be equal to unisex like so.
131
00:08:06,330 --> 00:08:08,730
And then now what we can do is we can actually grab this enum.
132
00:08:08,730 --> 00:08:13,410
And instead of saying that this is going to be a generic string, we can just define that this is going
133
00:08:13,410 --> 00:08:15,120
to be of type gender.
134
00:08:15,630 --> 00:08:24,060
So now instead of saying String Girl, we'll say gender dot girl, like, so now we can actually do
135
00:08:24,060 --> 00:08:25,350
the exact same thing.
136
00:08:26,450 --> 00:08:27,950
For the popularity.
137
00:08:29,290 --> 00:08:34,360
So popularity as well as the length.
138
00:08:35,170 --> 00:08:40,000
So for the popularity, we are going to figure out the options.
139
00:08:40,000 --> 00:08:46,570
I think it was trendy and then also over here forgot the option for this.
140
00:08:46,570 --> 00:08:47,830
Again, I think it was unique.
141
00:08:48,820 --> 00:08:50,990
Unique like so.
142
00:08:51,410 --> 00:08:53,950
So let's go ahead and change the value of those as well.
143
00:08:53,950 --> 00:08:56,560
So we're going to say trendy.
144
00:08:57,960 --> 00:08:59,730
And then unique.
145
00:09:00,630 --> 00:09:07,050
And then over here for the length we're going to have, shorts is equal to shorts.
146
00:09:08,870 --> 00:09:10,190
Short like so.
147
00:09:11,000 --> 00:09:13,220
We're also going to have long.
148
00:09:13,310 --> 00:09:15,880
That's equal to long.
149
00:09:16,580 --> 00:09:22,370
And lastly, we're going to have all that's equal to all like so.
150
00:09:23,030 --> 00:09:27,530
So over here, what we can do is say popularity.
151
00:09:27,530 --> 00:09:29,600
And then over here we can say length.
152
00:09:30,110 --> 00:09:36,570
So again now if you want to specify something, you're going to have to explicitly say length, dot,
153
00:09:36,590 --> 00:09:39,740
whatever it is, you don't have the option to add length, dot, medium.
154
00:09:39,740 --> 00:09:41,630
You only have these options right over here.
155
00:09:42,080 --> 00:09:45,770
So ensures that, well, we're going to pick the right thing.
156
00:09:45,770 --> 00:09:48,530
So trendy like so, so there we go.
157
00:09:48,530 --> 00:09:50,510
So this is really, really good.
158
00:09:50,870 --> 00:09:54,800
Now, of course, the application is working the way we expected because we didn't apply these types
159
00:09:54,800 --> 00:09:55,640
over here.
160
00:09:56,090 --> 00:09:58,370
So what I want to do is actually want to get rid of this object.
161
00:09:58,370 --> 00:10:00,170
This is just like kind of a test object.
162
00:10:00,170 --> 00:10:01,070
We don't really need that.
163
00:10:01,370 --> 00:10:07,340
But what I want to do is I want to grab this type and apply it to this reactive state.
164
00:10:08,180 --> 00:10:12,440
In order to do that, you might be thinking we can just do all the same thing that we did over here
165
00:10:12,470 --> 00:10:14,630
so we could do just colon and then that.
166
00:10:15,050 --> 00:10:17,390
That's actually not how it works.
167
00:10:17,630 --> 00:10:21,740
And to prove it to you, let's just go back, get that object that we had before.
168
00:10:22,040 --> 00:10:28,460
Let's copy that and let's paste that in there and then over here.
169
00:10:29,580 --> 00:10:30,660
Let's do.
170
00:10:31,870 --> 00:10:32,830
Colon.
171
00:10:32,950 --> 00:10:34,410
And then this actually.
172
00:10:34,430 --> 00:10:35,710
Well, I guess that does work.
173
00:10:35,890 --> 00:10:40,180
But the way that I prefer doing it is over here, right in the reactive.
174
00:10:40,420 --> 00:10:46,120
What you can do is you can add these brackets, these triangle brackets, and then you can add the state
175
00:10:46,120 --> 00:10:50,110
right in there and that accomplishes the exact same thing.
176
00:10:50,830 --> 00:10:56,920
So now again, if you decided to put in like a random string, it's going to yell at us.
177
00:10:57,160 --> 00:10:58,330
Let's go ahead and save this.
178
00:10:58,840 --> 00:11:04,480
And right now, you can see it's actually working the exact same way because these values over here,
179
00:11:04,480 --> 00:11:08,410
the correspond to these values right here.
180
00:11:08,680 --> 00:11:13,510
Now, to actually make this a little bit better, what we can do is we can actually grab these genomes
181
00:11:13,510 --> 00:11:15,910
and we can utilize them inside of these classes.
182
00:11:16,270 --> 00:11:21,370
So instead of saying that the options dot gender is equal to the string boy, we can say that this is
183
00:11:21,370 --> 00:11:23,110
going to be equal to gender.
184
00:11:23,110 --> 00:11:26,050
Dot and then boy like so.
185
00:11:26,590 --> 00:11:27,940
And this should work.
186
00:11:28,240 --> 00:11:28,810
Okay.
187
00:11:29,080 --> 00:11:30,620
So this would work the exact same way.
188
00:11:30,640 --> 00:11:36,220
So over here you can say gender, diet, you need sex.
189
00:11:37,150 --> 00:11:40,180
And then over here, gender.
190
00:11:41,290 --> 00:11:42,880
Dot girl.
191
00:11:43,300 --> 00:11:46,150
And if we save that again, it's working the exact same way.
192
00:11:46,180 --> 00:11:48,390
Girl is still highlighted over here.
193
00:11:49,150 --> 00:11:52,390
So let's go ahead and actually do that in here as well as we're going to say.
194
00:11:53,680 --> 00:12:01,000
We're going to say option or sorry, this is going to be popularity the enum popularity dots.
195
00:12:01,360 --> 00:12:03,160
This is which one is this one?
196
00:12:03,160 --> 00:12:03,850
Trendy.
197
00:12:04,780 --> 00:12:05,380
Okay.
198
00:12:06,010 --> 00:12:10,000
And then let's just copy that in and we're going to say unique here.
199
00:12:12,270 --> 00:12:13,320
Unique.
200
00:12:14,220 --> 00:12:16,100
And let's go right over here.
201
00:12:16,120 --> 00:12:18,240
We're going to say length.
202
00:12:18,550 --> 00:12:19,260
Dots.
203
00:12:20,560 --> 00:12:23,050
And over here, we're going to stay longer.
204
00:12:24,570 --> 00:12:25,560
Scrub that.
205
00:12:27,870 --> 00:12:32,100
NSA all over here so length that all.
206
00:12:32,670 --> 00:12:34,830
And then lastly, we're going to say length.
207
00:12:34,980 --> 00:12:36,920
Dot shorts.
208
00:12:39,270 --> 00:12:39,720
Okay.
209
00:12:40,230 --> 00:12:40,800
So again.
210
00:12:41,070 --> 00:12:44,100
Works exactly the same way.
211
00:12:44,550 --> 00:12:51,360
So this is looking great and I hope you guys actually learned quite a bit about TypeScript when doing
212
00:12:51,360 --> 00:12:51,660
this.
213
00:12:52,110 --> 00:13:00,540
And the reason why we would want to utilize TypeScript is is very, very powerful and important to utilize.
19562
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.