In last video we started to explore type annotations around simple types like numbers string boolean
and some built in objects as well.
We're not gonna write out a couple of examples that use some more complicated syntax.
So first start off with trying to add in a type invitation for an array in this example I'm going to
try to make a new variable called colors and I want to try to assign it a array of strings and maybe
these strings will be like red green and blue because hey we're calling this thing colors so to indicate
that I'll add on an annotation of string and then square brackets like so.
So this tells typescript that we're going to assign an array indicated by the square brackets.
That's going to contain nothing but strings inside of it.
So I can then place my equals sign and then replace the real array and say red green and blue so the
important thing here to understand is that the annotation and specifically this bracket right here is
not creating an array.
This is saying we're going to have something assigned to colors that is of type array that contains
strings one of the more complicated things in the world of typescript I think is kind of separating
all this new type indentation syntax and understanding when we are actually creating an array like as
we are here on the right hand side.
And when we are trying to indicate a type of array as we're doing here on the left hand side now we're
going to grow into great examples.
Much more detail with arrays in just a couple of videos.
So right now let's just kind of sit on this example right here and say if we ever want to create a type
annotation for an array of strings that's how we would do it.
We can do the same thing with say maybe an array of numbers as well so let's say How about let my numbers
for lack of a better name and I'll put down the type of value that I'm going to have inside of the array
which in this case will be a number then empty square brackets an equal sign and then my real array
like so the same thing with billions as well so I could say let I don't know truth's.
How about that.
That'll work that's going to be a boolean or an array of billions and I'll set equal to true true false.
Like so like I said we're going to go into greater detail with arrays in just a moment so we'll just
hang it right there.
So for our next example let's take a look at how we would deal with classes with type annotations.
So I'll put down another section here called classes and we'll create a class called car that's gonna
make an empty class like so classes are another thing that we're going to go into great detail in this
course.
Again for right now I just want you to understand how we can add in a type annotation with an instance
of class car so I could create a new variable down here and call it say lowercase car well then put
the colon and then the type of the variable or seeing the type of value that we're going to assign to
car.
In this case I want to make an instance of class car so I'm going to say that my type is capital c car
and this is where things start to get really crazy.
If you've never worked a lot with classes before traditionally we create classes with capitalized names.
So whenever you see a capital name like this like capital car that means we are referring to the type
of class car and then usually whenever we have a fully lowercase name as you see right here we have
a variable that's referring to an instance of a car.
So we're saying the variable car is only ever going to refer to an instance of a car we can then place
our equals sign and then make a new car.
And that's what creates our instance All right.
Now one more or two more quick examples here.
Take a look at how we add in a type notation for an object literal.
Now this is where the syntax starts to go a little bit more challenging but functions are even a little
bit worse.
So hey let's just hang in there for right now.
I want to try creating an object and adding in a type notation for it going to first create the object
and we'll discuss how we can add in the annotation so I'll make a new variable called points and I'm
going to assign that to an object with an x value of ten and a y value of about 20 like so.
So how would we add in a type invitation for this.
Well just like before we'll add in a colon and then we'll add in some syntax right here to describe
what type of value we're going to assign to point.
Now the only difference this time around is that the syntax is going to look a little bit nasty.
So to indicate that we're going to assign an object that has an x property that's a number and a y property
to number two the variable point we're gonna put the colon and then a set of curly braces and then we'll
list out all the different property names with their respective types right inside of that set of curly
braces.
So I'm going to say that the object that we're going to assign a point is going to have an x property
that's a number I'll then put in a semicolon and I'll say that it's going to also have a Y property
that's a number as well.
I only have to put in a semicolon when I'm separating different types inside of four year or a different
property names.
So I do not technically have to put in a colon after the Y property although I can't if I want to.
Like I said this is where things start to get a little bit crazy.
Once again we are declaring a variable called point.
We are assigning it an object that has an x property and y property and then we are simultaneously adding
in a type annotation for point as well and we're saying we can only ever assign an object to a point
that has an x property that's a number into y property that's a number as well so just as we saw a moment
ago back up you're at the top.
We saw very simply if we assign a bad value on the right hand side that doesn't agree with the type
on the left hand side.
We very quickly see an error message.
So that same rules applies with an object as well for example if I make the x property right here a
string instead I'll see an error message that says that I have the property called X but I'm trying
to provide a value that is a string instead of the number that I said I was going to provide so I'll
change that back over to 10.
Now the error goes away.
Likewise if I do not provide all the required property values like for example instead of Y eyesight
instead do something like a well see an error message as well.
And the error says hey you're trying to give something that has property a but we were expecting to
see somebody has Y instead so the same rule applies the syntax is just a little bit nastier.
All right.
Let's do one more quick pause in the next video we'll take a look at the syntax around functions and
we'll talk about why we care about all these type annotations so quick pause and I'll see you in just
a minute.