Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,020 --> 00:00:05,740
Now let's talk about the more complex types.
2
00:00:05,740 --> 00:00:08,620
So arrays and objects here.
3
00:00:08,620 --> 00:00:10,960
And let's start with arrays.
4
00:00:10,960 --> 00:00:15,890
If I have my hobbies, that might be an array of strings.
5
00:00:15,890 --> 00:00:18,710
Hence this type assignment is wrong.
6
00:00:18,710 --> 00:00:22,000
This allows me to store a single string and hobbies
7
00:00:22,000 --> 00:00:24,263
but I want to have an array of strings.
8
00:00:25,290 --> 00:00:28,660
To tell Typescript that I want an array of strings in there,
9
00:00:28,660 --> 00:00:32,290
we can instead assign this type string
10
00:00:32,290 --> 00:00:35,010
with square brackets thereafter.
11
00:00:35,010 --> 00:00:36,730
This tells TypeScript
12
00:00:36,730 --> 00:00:40,800
that we want to have an array of strings in hobbies.
13
00:00:40,800 --> 00:00:44,180
And of course we could have to same with an array of numbers
14
00:00:44,180 --> 00:00:46,590
and array of Booleans, whatever.
15
00:00:46,590 --> 00:00:48,620
But here I want to have an array of strings
16
00:00:48,620 --> 00:00:51,350
and that allows me to store
17
00:00:51,350 --> 00:00:54,340
an array of strings in there, like this.
18
00:00:54,340 --> 00:00:57,300
Now this is possible.
19
00:00:57,300 --> 00:01:00,320
If I would add a number, in addition to the strings,
20
00:01:00,320 --> 00:01:02,100
I again would get an error
21
00:01:02,100 --> 00:01:03,570
because that's not a string
22
00:01:03,570 --> 00:01:05,349
and I'm making it very clear
23
00:01:05,349 --> 00:01:09,820
that only an array of strings may be stored in hobbies.
24
00:01:09,820 --> 00:01:12,980
So that's how we can set up array types.
25
00:01:12,980 --> 00:01:16,720
Now we also can set types for entire objects.
26
00:01:16,720 --> 00:01:18,970
Let's say I have a person variable,
27
00:01:18,970 --> 00:01:22,570
and in there I don't wanna store a name or an age
28
00:01:22,570 --> 00:01:24,080
but an object.
29
00:01:24,080 --> 00:01:28,010
I want to be able to store something like this.
30
00:01:28,010 --> 00:01:32,520
An object with a name and an age.
31
00:01:32,520 --> 00:01:33,353
Something like that.
32
00:01:33,353 --> 00:01:35,163
That should be stored in person.
33
00:01:36,420 --> 00:01:38,700
Now, of course, this works like that
34
00:01:38,700 --> 00:01:41,430
because TypeScript out of the box
35
00:01:41,430 --> 00:01:44,650
without configuration is quite forgiving.
36
00:01:44,650 --> 00:01:48,470
And it by default allows me to store any value
37
00:01:48,470 --> 00:01:49,660
in a variable.
38
00:01:49,660 --> 00:01:52,950
It has a special any type for this,
39
00:01:52,950 --> 00:01:55,220
which we can see if we have our person.
40
00:01:55,220 --> 00:01:58,760
The default type assumed is the any type
41
00:01:58,760 --> 00:02:01,760
which we can all say explicitly set like this.
42
00:02:01,760 --> 00:02:03,970
Which simply tells TypeScript,
43
00:02:03,970 --> 00:02:05,500
I don't wanna tell you anything
44
00:02:05,500 --> 00:02:08,780
about the types of values stored in this variable.
45
00:02:08,780 --> 00:02:10,789
Anything is allowed.
46
00:02:10,789 --> 00:02:14,350
And that of course is a fallback type,
47
00:02:14,350 --> 00:02:16,540
which you typically should not use
48
00:02:16,540 --> 00:02:19,340
because that kind of defeats the entire purpose
49
00:02:19,340 --> 00:02:21,840
of using TypeScript in the first place.
50
00:02:21,840 --> 00:02:23,250
With the any type,
51
00:02:23,250 --> 00:02:27,160
you are basically back to standard Javascript.
52
00:02:27,160 --> 00:02:28,180
So it's the default
53
00:02:28,180 --> 00:02:31,150
but of course it's not what I wanna keep here.
54
00:02:31,150 --> 00:02:33,670
Instead I wanna make it very clear
55
00:02:33,670 --> 00:02:37,450
which kind of object can be stored in person.
56
00:02:37,450 --> 00:02:40,340
I wanna allow this object for example
57
00:02:40,340 --> 00:02:43,690
but storing another object with a different structure.
58
00:02:43,690 --> 00:02:47,170
Where I have let's say just a Boolean in there.
59
00:02:47,170 --> 00:02:49,930
That's something I don't wanna allow.
60
00:02:49,930 --> 00:02:52,780
And for this, we can again add a type assignment
61
00:02:52,780 --> 00:02:56,010
but now we wanna add an object type.
62
00:02:56,010 --> 00:02:58,810
A type defining an object.
63
00:02:58,810 --> 00:03:01,730
And for this, we then add curly-braces
64
00:03:01,730 --> 00:03:04,630
just as we do it when we create an object.
65
00:03:04,630 --> 00:03:06,870
But please note that this is not
66
00:03:06,870 --> 00:03:09,300
on the right side of an equal sign here.
67
00:03:09,300 --> 00:03:11,790
So we're not creating our object here.
68
00:03:11,790 --> 00:03:15,420
Instead here, it's in the place where we set a type
69
00:03:15,420 --> 00:03:18,260
for a variable on the right side of this colon
70
00:03:18,260 --> 00:03:20,313
after the variable name.
71
00:03:20,313 --> 00:03:22,990
And thefore this here is actually a type assignment.
72
00:03:22,990 --> 00:03:25,380
And hence here, we're not creating an object
73
00:03:25,380 --> 00:03:28,450
but instead we're using a TypeScript feature here
74
00:03:28,450 --> 00:03:31,760
which is the object type definition.
75
00:03:31,760 --> 00:03:34,520
So we're defining the type for an object.
76
00:03:34,520 --> 00:03:37,620
We're defining the structure of an object.
77
00:03:37,620 --> 00:03:41,210
And here we can then for example, say that person
78
00:03:41,210 --> 00:03:44,280
should store objects that have a name field,
79
00:03:44,280 --> 00:03:48,020
where the value of that name field is a string
80
00:03:48,020 --> 00:03:51,930
and which have a age field where the value is number.
81
00:03:51,930 --> 00:03:55,810
So where the value is of type number, I should say.
82
00:03:55,810 --> 00:04:00,150
And with that, we're telling TypeScript that only objects
83
00:04:00,150 --> 00:04:03,460
that have this kind of type should be storable
84
00:04:03,460 --> 00:04:05,510
in that variable.
85
00:04:05,510 --> 00:04:08,540
That's why I can store this object here
86
00:04:08,540 --> 00:04:12,360
because that object down there has exactly the structure
87
00:04:12,360 --> 00:04:13,900
I'm defining here.
88
00:04:13,900 --> 00:04:16,940
It has a name field, which has a string as a value
89
00:04:16,940 --> 00:04:20,640
and a age field, which has a number as a value
90
00:04:20,640 --> 00:04:21,790
On the otherhand,
91
00:04:21,790 --> 00:04:25,250
this object down here is not storable.
92
00:04:25,250 --> 00:04:27,120
And hence, I'm getting an error here
93
00:04:27,120 --> 00:04:29,950
because it is having the wrong type.
94
00:04:29,950 --> 00:04:32,210
This objects type would be
95
00:04:32,210 --> 00:04:34,920
that it has an isEmployee field,
96
00:04:34,920 --> 00:04:37,560
which stores a value of type Boolean.
97
00:04:37,560 --> 00:04:41,490
Which does not match this type description here.
98
00:04:41,490 --> 00:04:43,520
So, comment this out.
99
00:04:43,520 --> 00:04:47,593
And that's how we can define array types and object types.
100
00:04:48,600 --> 00:04:51,130
Of course, we could also combine these two.
101
00:04:51,130 --> 00:04:54,430
If I wanted to have an array full of people.
102
00:04:54,430 --> 00:04:59,180
So full off people that have this type signature here,
103
00:04:59,180 --> 00:05:03,270
then I could set the type of people here
104
00:05:03,270 --> 00:05:05,420
to this object type.
105
00:05:05,420 --> 00:05:06,910
So I'm repeating this
106
00:05:06,910 --> 00:05:11,380
but as an array by adding square brackets thereafter.
107
00:05:11,380 --> 00:05:14,290
Now I'm saying in people,
108
00:05:14,290 --> 00:05:17,930
I don't wanna store a single object of that structure,
109
00:05:17,930 --> 00:05:21,103
but an array of such objects.
110
00:05:22,050 --> 00:05:25,550
And that's how we can then already get a bit more advanced
111
00:05:25,550 --> 00:05:28,830
by combining different TypeScript features
112
00:05:28,830 --> 00:05:31,723
and by combining different types.
8706
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.