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