Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,066 --> 00:00:03,366
- [Lecturer] Now, there are a lot of
2
00:00:03,366 --> 00:00:05,900
other core TypeScript features
3
00:00:05,900 --> 00:00:08,132
which I do explore in greater detail
4
00:00:08,132 --> 00:00:10,033
in my TypeScript course.
5
00:00:10,033 --> 00:00:13,233
To come to an end here
for this introduction,
6
00:00:13,233 --> 00:00:16,000
so that we can move to our react example
7
00:00:16,000 --> 00:00:19,100
and see how we connect
TypeScript with react,
8
00:00:19,100 --> 00:00:22,833
there are just two last features
9
00:00:22,833 --> 00:00:24,400
which I wanna cover here.
10
00:00:24,400 --> 00:00:28,100
And that would be functions
combined with types,
11
00:00:28,100 --> 00:00:31,400
and then thereafter, generic types.
12
00:00:31,400 --> 00:00:35,833
Let's start with functions and types.
13
00:00:35,833 --> 00:00:38,200
And I'm saying function and types
14
00:00:38,200 --> 00:00:40,566
because when we work with functions
15
00:00:40,566 --> 00:00:45,433
there are different places
where types can be assigned.
16
00:00:46,400 --> 00:00:48,200
Let's start by adding a function here.
17
00:00:48,200 --> 00:00:51,433
Let's say the add function
again which we saw before.
18
00:00:52,466 --> 00:00:56,133
The add function can get
parameters of course,
19
00:00:56,133 --> 00:00:59,566
like any function just as I did it before.
20
00:00:59,566 --> 00:01:03,233
And then for these parameters,
we can assign types.
21
00:01:03,233 --> 00:01:07,933
That's what we saw before
here in this first example.
22
00:01:07,933 --> 00:01:11,000
Just as we can set types for variables
23
00:01:11,000 --> 00:01:14,066
we can also set types for parameters.
24
00:01:14,066 --> 00:01:17,300
We can make it clear
that here we wanna allow
25
00:01:17,300 --> 00:01:19,300
numbers for example.
26
00:01:20,400 --> 00:01:22,600
And then I can return a plus b.
27
00:01:23,700 --> 00:01:25,266
But with that
28
00:01:25,266 --> 00:01:29,366
we also have one inferred
type here in this function.
29
00:01:29,366 --> 00:01:33,600
And do you see where we're
using type inference here?
30
00:01:34,700 --> 00:01:38,466
Well, here in this function,
I am returning a value
31
00:01:38,466 --> 00:01:43,466
and that tells TypeScript
something about the return type,
32
00:01:43,500 --> 00:01:46,966
the type of the returned
value of this function.
33
00:01:46,966 --> 00:01:49,900
And it infers this type.
34
00:01:49,900 --> 00:01:54,100
We can see the type it
infers if we hover over add.
35
00:01:54,100 --> 00:01:57,833
Then we see the TypeScript
definition of this function
36
00:01:57,833 --> 00:02:00,600
and we see that, of
course, for the parameters,
37
00:02:00,600 --> 00:02:01,966
we have the two types.
38
00:02:01,966 --> 00:02:04,833
We set up the number types, but then here
39
00:02:04,833 --> 00:02:09,333
after this function name
and the parameter list,
40
00:02:09,333 --> 00:02:12,333
this colon here, and this type here,
41
00:02:12,333 --> 00:02:13,900
that's not coming from us.
42
00:02:13,900 --> 00:02:15,733
We haven't added this code.
43
00:02:15,733 --> 00:02:19,866
Instead that's the
inferred return value type
44
00:02:19,866 --> 00:02:22,133
TypeScript infers here.
45
00:02:22,133 --> 00:02:26,933
So TypeScript knows that
here it will get a number
46
00:02:26,933 --> 00:02:30,900
as a return value because
it sees that this code
47
00:02:30,900 --> 00:02:34,833
with these parameters which
are always of type number
48
00:02:34,833 --> 00:02:36,766
can only return a number.
49
00:02:36,766 --> 00:02:39,900
There's no other value
that could be returned.
50
00:02:39,900 --> 00:02:44,200
And that's why this number
return value is inferred.
51
00:02:44,200 --> 00:02:46,900
We could also explicitly set it though.
52
00:02:46,900 --> 00:02:50,633
We can set the return type of a function
53
00:02:50,633 --> 00:02:54,933
by adding a colon after
this function parameter list
54
00:02:54,933 --> 00:02:57,200
and then setting the return type here,
55
00:02:57,200 --> 00:03:00,966
like number, or a string, or a union type,
56
00:03:00,966 --> 00:03:02,533
that would also be possible.
57
00:03:03,600 --> 00:03:07,266
That's something we can
do, but just as before,
58
00:03:07,266 --> 00:03:09,533
if you don't have a reason for doing that
59
00:03:09,533 --> 00:03:10,900
you shouldn't do that.
60
00:03:10,900 --> 00:03:13,566
TypeScript already infers a type here.
61
00:03:13,566 --> 00:03:15,933
And if that's the type you wanna stick to
62
00:03:15,933 --> 00:03:19,933
then there's no need to
explicitly define it yourself.
63
00:03:19,933 --> 00:03:23,300
Still, I wanna mention
this, that functions,
64
00:03:23,300 --> 00:03:26,600
when we work with types,
don't just use types
65
00:03:26,600 --> 00:03:30,466
for the parameters, but
also for the return value.
66
00:03:30,466 --> 00:03:33,666
That's easy to overlook,
but it makes a lot of sense
67
00:03:33,666 --> 00:03:38,066
because a function after all
does not have just some input.
68
00:03:38,066 --> 00:03:41,900
It also has some output and
hence it has a return type.
69
00:03:42,866 --> 00:03:44,966
Now there is a special return type
70
00:03:44,966 --> 00:03:47,000
which is also worth knowing.
71
00:03:47,000 --> 00:03:50,133
And for that, let me add a print function
72
00:03:50,133 --> 00:03:54,400
where I get a value and that
could indeed be of type any
73
00:03:54,400 --> 00:03:56,900
because the only thing
I'll do with that value
74
00:03:56,900 --> 00:03:59,433
is I'll console log it.
75
00:03:59,433 --> 00:04:03,100
So I don't really care
about the concrete type
76
00:04:03,100 --> 00:04:04,000
of this value.
77
00:04:04,000 --> 00:04:05,200
I'm just logging it.
78
00:04:05,833 --> 00:04:09,666
Now one important note about naming this function "print" though,
79
00:04:09,666 --> 00:04:14,466
which I unfortunately only discovered after I named the function like this.
80
00:04:14,466 --> 00:04:18,766
Of course, in JavaScript, there's a built-in "print" function already,
81
00:04:18,766 --> 00:04:22,566
so now this function will clash with this built-in function
82
00:04:22,600 --> 00:04:26,133
and hence if you would try to compile this TypeScript file
83
00:04:26,166 --> 00:04:28,566
you would be getting an error because of that,
84
00:04:28,566 --> 00:04:33,000
so maybe just name it "printOutput" or something like that.
85
00:04:33,100 --> 00:04:35,433
to avoid that error!
86
00:04:36,000 --> 00:04:38,833
But the important thing
about this print function
87
00:04:38,833 --> 00:04:43,533
now is that this function
does not return anything.
88
00:04:43,533 --> 00:04:47,033
It has no return statement
and therefore it has
89
00:04:47,033 --> 00:04:52,033
a special return type called void.
90
00:04:52,633 --> 00:04:57,000
Void is basically comparable
to nul and undefined,
91
00:04:57,000 --> 00:04:59,966
but it's only used in
conjunction with functions
92
00:04:59,966 --> 00:05:04,266
and it means that this
function never returns.
93
00:05:04,266 --> 00:05:06,833
So if I wanna work with the returned value
94
00:05:06,833 --> 00:05:10,600
from that function, I
will work with undefined
95
00:05:10,600 --> 00:05:14,400
and that's that special
wide type for functions,
96
00:05:14,400 --> 00:05:17,133
for the return value of functions.
7825
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.