I know you spent a lot of time talking about annotations and inference but we've got just one last little
sub topic to discuss which is objects.
Now this discussion is going to go a lot more quickly because all we're really discussing here is how
we do stuff like object structuring which we kind of saw an example of in argument lists just a second
ago.
So let's just flip directly back over to our code editor and talk about how to work with objects around
this annotation stuff.
So instead my annotations directory I'm going to make another file called objects not T.S. and then
inside of here.
I want to declare a new object called profile and this will be a profile for a person so maybe they
have a name like Alex.
Maybe they have an age like 20 and we'll say that this person also has some coordinates like their physical
location in the world.
So I'm going to abbreviate that as simply chords and then I'll give them a latitude and longitude property
inside there.
So we've got a nested object right here.
We've got the latitude and longitude representing this person's physical location in the world now last
thing on out this object is a method a method that will change the age of this person.
So obviously we could do something like you know profile that age equals 20.
Well let's make a separate function just to have a nice function to call all right.
So one more property will add in here is set age and rather than defining this as like a function like
so I want to try using the Yes 2015 syntax for defining a method inside insurgent object.
So instead I'll say set age.
I'll put my argument list in and then the function body so let's try adding in some annotation for this
function right here using the same rules that we just learned.
So I'm going to put in a age as an argument and then the type of this argument which will be a number
if we're just updating this person's age I don't really expect to return anything from this function.
So I'm going to entertain the return type is being void like so so then we can write out our implementation
inside the function.
We could say this dot age is age all right.
So that's an example of a function being defined inside of an object with the function annotation on
it.
So let's now try doing a little bit of yes 2015 structuring and pulling some different properties out
of this profile object.
So for example let's try D structuring out the age property right here and figure out what the exact
syntax for that would be so of course we could do something like const age is profile that age.
Like I said what we want to use these structuring instead.
So instead I'm going to write out just profile and we can do normal D structuring like so now if we
really wanted to put a type annotation on here for some reason like maybe we fall into one of those
three categories that we discussed earlier right here if we really wanted to put on some annotation
directly onto the variable right here we could do so by writing out a colon and then the structure of
the profile object so we would not just say oh this thing is going to have like age that is a number
instead we have to list out the structure of profile any different properties that we wanted to have.
So in this case we would put down curly braces age and no like so so notice how this seems to kind of
like violate the rules of annotations previously when we're talking about variable annotations we had
said well yeah we just put the type of the variable or the type of value that we're going to assign
to this variable.
So we're creating a variable right here called H.
And so you might think that we only have to label it with the type that age is going to refer to.
Which would be no that's not the case if we want to use these structuring we have to write out the expected
structure of profile over here on the right hand side.
So I expect this value right here to have a structure that is an object with an age property that is
a number now to make things just a little bit more complicated.
Let's try.
Also D structuring out our latitude and longitude as well.
So this is going to be some nasty stuff but I'll be some pretty good practice.
So we would write out one set of curly braces we would say look at the court's property and then tried
to pull out latitude and longitude out of there.
And we want to get all that from the profile object like so okay.
So if you're just doing plain destruction with an out in the annotation that is what it would look like
to be just identical to the normal.
Yes 2015 syntax.
But once again if we fell into one these conditions where we had to put in an annotation or we would
have to do is once again kind of mirror the object structure.
Do we expect profile to be so it's going to be a little bit nasty here right after the deal structuring
we can put in a colon.
Then we'll start to describe the structure of the profile object.
It's all put a set of curly braces I'm going to say that this thing is going to have property cords
that's going to be an object and inside of that inner object we've got a lot property that is a no and
an LNG property that is a number as well as a really long line so I'm going to say the file really quickly
just to get it to reformat so you can see the whole line in total so once again kind of unexpectedly
we do not just list out the expected type of lat and long.
And I think this example right here will give you a better idea of why that is if we think consider
backup here if we had only put number well what would happen if we wanted to D structure multiple properties
and also pull out say this person's name as well which was the string if this was the case is number
referring to the type of age or name.
So this is why we have to kind of double up on the structure whenever we are doing the restructuring.
So age is a number and name would be a string we have to label these type of all the different properties
coming out of that object now in this case in this I've got a little error here around name that's really
fine it's just because the way my project is structured chances are you see an error here as well that
says something like name was also declared.
If you see that totally fine you can't ignore it all right.
That's it.
Around objects like I said I just wanted to very quickly talk about D structuring around objects and
what annotations for them look like and also how we talk in function declarations inside or there as
well with some annotations.
OK so that's pretty much it.
On type annotations and type inference.
So we've gone over several plain definitions win over some examples we spoke about why we care about
these remembers because we want to make sure typescript is aware the type of all the variables we work
with and we also discussed when we're going to use annotations and when we're going to fall back to
inference whenever working with functions we're going to rely upon annotations 100 percent of time for
both arguments and return types when we're working with variables we're going to rely upon type inference
as much as we possibly can unless we are relying upon or in one of these three scenarios right here.
All right so now we've got through a lot of this kind of boring stuff.
Let's take a quick pause right here and continue on our next subject in the next video.