Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:04,160
So what is TypeScript
2
00:00:04,160 --> 00:00:06,770
and why would you use it?
3
00:00:06,770 --> 00:00:11,770
TypeScript is a so-called "superset" to JavaScript.
4
00:00:11,800 --> 00:00:16,650
It's a programming language which builds up on JavaScript.
5
00:00:16,650 --> 00:00:19,644
It extends JavaScript you could say,
6
00:00:19,644 --> 00:00:22,050
and that is really important.
7
00:00:22,050 --> 00:00:26,424
The core programming language still, is JavaScript.
8
00:00:26,424 --> 00:00:30,060
So the base JavaScript syntax you know,
9
00:00:30,060 --> 00:00:32,530
how you write JavaScript code,
10
00:00:32,530 --> 00:00:37,530
if statements, for loops, objects, all that does not change.
11
00:00:37,770 --> 00:00:41,560
But TypeScript then adds more features
12
00:00:41,560 --> 00:00:46,180
to the JavaScript syntax, and that's also important.
13
00:00:46,180 --> 00:00:51,000
Unlike React, TypeScript is not a library for JavaScript,
14
00:00:51,000 --> 00:00:53,890
so it doesn't use JavaScript features
15
00:00:53,890 --> 00:00:57,360
to build new features on top of them,
16
00:00:57,360 --> 00:01:00,110
to build functionalities around them.
17
00:01:00,110 --> 00:01:04,349
But instead it extends the core JavaScript syntax,
18
00:01:04,349 --> 00:01:09,350
and most importantly, it adds static typing to JavaScript.
19
00:01:10,620 --> 00:01:14,220
And that's also where the name comes from, TypeScript.
20
00:01:14,220 --> 00:01:17,615
It adds static typing to JavaScript,
21
00:01:17,615 --> 00:01:22,615
because JavaScript on its own is actually dynamically typed.
22
00:01:23,280 --> 00:01:26,880
And what does this mean and why is this useful?
23
00:01:26,880 --> 00:01:30,440
For this, I prepared a very simple example here,
24
00:01:30,440 --> 00:01:32,250
which you find attached.
25
00:01:32,250 --> 00:01:34,470
This is not a React app,
26
00:01:34,470 --> 00:01:38,040
this is a standard JavaScript function
27
00:01:38,040 --> 00:01:43,000
and a standard HTML file, which has no actual content
28
00:01:43,000 --> 00:01:46,083
but which imports this script file.
29
00:01:47,190 --> 00:01:49,740
Now, if I open this file in the browser,
30
00:01:49,740 --> 00:01:52,410
simply by copying the path here
31
00:01:52,410 --> 00:01:55,690
and then opening that path in the browser,
32
00:01:55,690 --> 00:01:58,780
if I reload this page and open the developer tools,
33
00:01:58,780 --> 00:02:01,940
I see seven here in the developer tools,
34
00:02:01,940 --> 00:02:04,200
in the JavaScript console.
35
00:02:04,200 --> 00:02:07,935
And I see seven here because in this script file,
36
00:02:07,935 --> 00:02:10,130
I define an "add" function,
37
00:02:10,130 --> 00:02:12,170
and then I call this "add" function
38
00:02:12,170 --> 00:02:14,510
and store the result in this constant
39
00:02:14,510 --> 00:02:18,170
and then I log this constant.
40
00:02:18,170 --> 00:02:22,672
So that's standard JavaScript, nothing fancy here.
41
00:02:22,672 --> 00:02:26,040
Now JavaScript has types.
42
00:02:26,040 --> 00:02:29,360
JavaScript knows the concept of types.
43
00:02:29,360 --> 00:02:32,540
For example, here, the two values which I'm passing in,
44
00:02:32,540 --> 00:02:34,950
are of type "number",
45
00:02:34,950 --> 00:02:37,470
and that's something JavaScript knows
46
00:02:37,470 --> 00:02:40,370
on its own, without TypeScript.
47
00:02:40,370 --> 00:02:42,660
The important thing just is that,
48
00:02:42,660 --> 00:02:47,160
JavaScript as I just mentioned, is dynamically typed.
49
00:02:47,160 --> 00:02:49,830
Which means that this function here,
50
00:02:49,830 --> 00:02:53,180
does not expect any particular types.
51
00:02:53,180 --> 00:02:56,410
It just knows that it will receive two parameters,
52
00:02:56,410 --> 00:02:57,870
and that's it.
53
00:02:57,870 --> 00:02:59,790
So it's not statically typed,
54
00:02:59,790 --> 00:03:04,400
the types for this function are not announced ahead of time,
55
00:03:04,400 --> 00:03:06,820
instead it just takes whatever it gets
56
00:03:06,820 --> 00:03:09,730
and then tries to execute this code.
57
00:03:09,730 --> 00:03:13,400
And that code happens to work with numbers.
58
00:03:13,400 --> 00:03:18,170
But this code also happens to work with strings.
59
00:03:18,170 --> 00:03:22,200
And hence, if I convert my two numbers into strings here,
60
00:03:22,200 --> 00:03:24,470
by wrapping them with quotes,
61
00:03:24,470 --> 00:03:27,300
then I got two different types here,
62
00:03:27,300 --> 00:03:29,930
I got two strings instead of numbers,
63
00:03:29,930 --> 00:03:32,430
which are types JavaScript knows.
64
00:03:32,430 --> 00:03:35,070
And if I change it like this and saved as file,
65
00:03:35,070 --> 00:03:39,310
if I reload, you see now we get 25 here,
66
00:03:39,310 --> 00:03:41,710
because now the plus operator
67
00:03:41,710 --> 00:03:46,190
does not add these two arguments in a mathematical way,
68
00:03:46,190 --> 00:03:49,000
but instead it concatenates two strings
69
00:03:49,000 --> 00:03:52,200
because it receives two strings here.
70
00:03:52,200 --> 00:03:57,200
And that shows why just JavaScript is okay and great,
71
00:03:57,310 --> 00:04:00,960
but why it might not always be best.
72
00:04:00,960 --> 00:04:05,930
Why having static typing could enhance your code
73
00:04:05,930 --> 00:04:09,060
and prevent errors like this.
74
00:04:09,060 --> 00:04:11,600
Now, of course you might say, "Which error?
75
00:04:11,600 --> 00:04:14,250
I can simply not call "add" like this,
76
00:04:14,250 --> 00:04:17,570
I just make sure that I always pass into numbers,
77
00:04:17,570 --> 00:04:19,649
and it won't face this problem."
78
00:04:19,649 --> 00:04:22,480
And that's true, but in a bigger project,
79
00:04:22,480 --> 00:04:24,260
with lots of code files,
80
00:04:24,260 --> 00:04:27,740
and potentially a lot of people working on the code base,
81
00:04:27,740 --> 00:04:30,270
you might sometimes call a function
82
00:04:30,270 --> 00:04:34,770
or use some object in an unintended way.
83
00:04:34,770 --> 00:04:37,540
And then you could run into problems like this,
84
00:04:37,540 --> 00:04:39,770
because nothing is warning you
85
00:04:39,770 --> 00:04:43,140
that this is not how you should use this function.
86
00:04:43,140 --> 00:04:44,980
Nothing is telling you,
87
00:04:44,980 --> 00:04:48,990
that this function actually wants two numbers,
88
00:04:48,990 --> 00:04:52,820
instead of two strings or any values.
89
00:04:52,820 --> 00:04:56,630
And that's where TypeScript can help us.
90
00:04:56,630 --> 00:05:00,800
Because with TypeScript, and I'll show you this code
91
00:05:00,800 --> 00:05:03,510
even though we haven't installed TypeScript yet,
92
00:05:03,510 --> 00:05:05,980
but I have it installed on my system already,
93
00:05:05,980 --> 00:05:09,630
and we will install it together in the next lecture too.
94
00:05:09,630 --> 00:05:11,560
But let me show you this first.
95
00:05:11,560 --> 00:05:14,510
So with TypeScript, if I copy this code,
96
00:05:14,510 --> 00:05:17,610
what we will be able to do is,
97
00:05:17,610 --> 00:05:20,760
we can add type annotations here,
98
00:05:20,760 --> 00:05:23,940
simply by adding a colon after our parameter,
99
00:05:23,940 --> 00:05:25,830
and then adding the type
100
00:05:25,830 --> 00:05:28,260
which should be used for this parameter.
101
00:05:28,260 --> 00:05:30,510
I'll do this for B as well.
102
00:05:30,510 --> 00:05:34,330
And we'll also not just be able to use type annotations
103
00:05:34,330 --> 00:05:36,150
on function parameters,
104
00:05:36,150 --> 00:05:38,820
but as you will see throughout this module,
105
00:05:38,820 --> 00:05:42,053
we can use types and many other situations as well.
106
00:05:42,980 --> 00:05:44,710
So now I am making it clear,
107
00:05:44,710 --> 00:05:49,710
with TypeScript that here, I actually want two parameters
108
00:05:50,350 --> 00:05:52,900
which both should be of type number.
109
00:05:52,900 --> 00:05:56,480
And hence, I already get an error here in my IDE,
110
00:05:56,480 --> 00:05:59,040
which tells me that this argument of type string
111
00:05:59,040 --> 00:06:01,963
is not assignable to a parameter of type number,
112
00:06:02,900 --> 00:06:04,220
pretty helpful.
113
00:06:04,220 --> 00:06:08,110
And with that, I can catch such an error,
114
00:06:08,110 --> 00:06:11,800
such an unintended use of this function
115
00:06:11,800 --> 00:06:15,060
before I run and test my code.
116
00:06:15,060 --> 00:06:16,480
And that of course allows us
117
00:06:16,480 --> 00:06:18,610
to write better code in the end,
118
00:06:18,610 --> 00:06:20,650
because we can avoid such errors
119
00:06:20,650 --> 00:06:23,300
and we don't have to track them at runtime
120
00:06:23,300 --> 00:06:27,800
but we see such errors right, when we start writing code.
121
00:06:27,800 --> 00:06:31,953
And that's why using TypeScript could be worth a thought.
9577
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.