0
1
00:00:00,000 --> 00:00:03,661
Okay so in the video we're
going to talk about objects. So
1

2
00:00:03,661 --> 00:00:06,701
we're going to use them a lot
so that's still a core
2

3
00:00:06,701 --> 00:00:10,941
concept. Uh so I'm going to
show you how to type an object.
3

4
00:00:10,941 --> 00:00:16,181
So let's say we have a print
coordinates function. So
4

5
00:00:16,181 --> 00:00:20,861
coordinates can be made of a
latitude and longitude. So we
5

6
00:00:20,861 --> 00:00:24,781
could expect an object in this
function. So I'm going to call
6

7
00:00:24,781 --> 00:00:29,421
that coords and I'm going to
type it as an object. And I
7

8
00:00:29,421 --> 00:00:34,221
will say and I will explicitly
provide my type by saying that
8

9
00:00:34,221 --> 00:00:37,821
it's going to be called lat and
it's going to be for example a
9

10
00:00:37,821 --> 00:00:42,301
string and a longitude that is
going to be a string so that's
10

11
00:00:42,301 --> 00:00:45,661
how you can type an object on
the fly directly in your
11

12
00:00:45,661 --> 00:00:48,541
parameters okay you can define
your type like this and then
12

13
00:00:48,541 --> 00:00:54,061
for example I could just
console log my lat that would
13

14
00:00:54,061 --> 00:01:01,321
be coords.lat and lng and that
will work well so you'll see
14

15
00:01:01,321 --> 00:01:07,541
that later we will find a way
to define our type outside and
15

16
00:01:07,541 --> 00:01:10,741
reuse them because if I want to
reuse my coords somewhere I
16

17
00:01:10,741 --> 00:01:14,021
will have to.. I will have to type
them again okay in another
17

18
00:01:14,021 --> 00:01:20,741
function so for example I don't
know get distance between and I
18

19
00:01:20,741 --> 00:01:24,741
would have a coords one and a
coords two and let's say this
19

20
00:01:24,741 --> 00:01:31,861
function returns the distance
between coords one
20

21
00:01:32,721 --> 00:01:38,661
and coords two, well you would
have to do this again.
21

22
00:01:41,221 --> 00:01:46,201
And so as you can see that can
rapidly become a waste of time
22

23
00:01:46,201 --> 00:01:50,761
to repeat the types and
redefine them everywhere so I
23

24
00:01:50,761 --> 00:01:55,001
will later show you a way to
declare a type and reuse it
24

25
00:01:55,001 --> 00:02:00,601
anywhere now let's say I create
a user , so as you can see
25

26
00:02:00,601 --> 00:02:03,881
I've created an object user
with some values and when I
26

27
00:02:03,881 --> 00:02:07,481
pass my mouse over since I have
declared my object directly and
27

28
00:02:07,481 --> 00:02:11,561
sent my values the inference is
working and as you can see my
28

29
00:02:11,561 --> 00:02:16,121
type has already been found by
typescript so I can just do a
29

30
00:02:16,121 --> 00:02:19,241
user. and as you can see I have
last name that is a string and
30

31
00:02:19,241 --> 00:02:23,081
my name that is a string and
let's say I decide to create a
31

32
00:02:23,081 --> 00:02:27,881
function that is displayUser
like this that is going to
32

33
00:02:27,881 --> 00:02:31,881
receive a user that is an
object that contain for example
33

34
00:02:31,881 --> 00:02:38,041
a first name that is a string
and the last name that is a
34

35
00:02:38,041 --> 00:02:42,201
string like this so I'm going
to change it here last name so
35

36
00:02:42,201 --> 00:02:47,161
it will match and inside let's
say I just display I don't know
36

37
00:02:47,161 --> 00:02:55,201
the first name so user.
firstName and I'm going to do
37

38
00:02:55,201 --> 00:02:58,401
a toLowerCase for example
38

39
00:02:59,721 --> 00:03:05,181
And I'm going to do the same
but for the last name.
39

40
00:03:06,841 --> 00:03:14,601
user.lastName that to
lowerCase. So this is
40

41
00:03:14,601 --> 00:03:18,361
going to work fine, if I do
a display user and send my user
41

42
00:03:18,361 --> 00:03:25,721
here oh and I made a mistake
here this should be first name
42

43
00:03:26,221 --> 00:03:30,881
as you can see yes it's working
now I just want to show you
43

44
00:03:30,881 --> 00:03:33,681
that you can also make an
attribute optional so for
44

45
00:03:33,681 --> 00:03:37,201
example let's say I say that my
last name is optional I can do
45

46
00:03:37,201 --> 00:03:41,761
that but as you can see well
since this can be undefined as
46

47
00:03:41,761 --> 00:03:44,601
you can see now the type of
last name is string or
47

48
00:03:44,601 --> 00:03:48,241
undefined typescript doesn't
know but, it knows that it
48

49
00:03:48,241 --> 00:03:53,601
can be one of these okay, so you
cannot call toLocaleLowerCase
49

50
00:03:53,601 --> 00:03:57,801
because that would be dangerous
okay if for some reason my My
50

51
00:03:57,801 --> 00:04:00,841
user doesn't have a last name
this is going to crash so what
51

52
00:04:00,841 --> 00:04:05,801
it can do is you can just check
if the user.lastName is
52

53
00:04:05,801 --> 00:04:09,401
defined and just by doing that
typescript is going to
53

54
00:04:09,401 --> 00:04:12,841
understand that all right now
here in this piece of code in
54

55
00:04:12,841 --> 00:04:15,721
this section there is no chance
that last name is undefined as
55

56
00:04:15,721 --> 00:04:19,961
you can see the type changes
because now it's it has to be a
56

57
00:04:19,961 --> 00:04:23,441
string because it cannot be
undefined in this if statement
57

58
00:04:23,441 --> 00:04:28,201
so you can do it like this or
you can use a shorter syntax
58

59
00:04:28,201 --> 00:04:32,241
and just use the question mark
like this this is going to
59

60
00:04:32,241 --> 00:04:36,241
check in the exact same way if
the last name is defined or not
60

61
00:04:36,241 --> 00:04:41,121
if it's defined then yes you
will be able to call to local
61

62
00:04:41,121 --> 00:04:44,321
lowercase and if it's not then
nothing is going to happen and
62

63
00:04:44,321 --> 00:04:47,761
it's just going to return
undefined and that's it okay
63

64
00:04:47,761 --> 00:04:50,161
but this way it's not going to
crash it's not going to call
64

65
00:04:50,161 --> 00:04:53,201
the function if it's undefined.
Alright so that is pretty much
65

66
00:04:53,201 --> 00:04:57,361
it about objects obviously this
may still be a little bit
66

67
00:04:57,361 --> 00:05:00,241
blurry okay for you because we
haven't really used them in a
67

68
00:05:00,241 --> 00:05:03,261
real project but we're going to
do that later, don't worry.
68

69
00:05:03,261 --> 00:05:06,301
Alright, so that's pretty much
about objects. See you in the
69

70
00:05:06,301 --> 00:05:08,701
next one.
