In this video we're going to write out a couple of code examples to understand how type annotations
work specifically with variables.
Remember a type annotation is going to be a little bit of code that we write to help typescript along
the type annotation system is kind of at odds with type inference.
So we're going to first focus on type annotations and then come along and understand how type inference
comes into play.
All right.
So going to change on over to my editor and inside my project directory I'm going to make a new folder
called annotations.
We're going to eventually create separate files inside this directory that are all related to type annotations.
So we're going to organize them all inside of here inside this file during went to make a new file called
variables dot t s and then inside this file I'm going to write out my first variable declaration that
has a type annotation.
So I'm going to write out const apples is going to be a number I'm going to set that to five like so
so the colon and the word number right after it is our type annotation you're going to see syntax that
looks exactly like this in the vast majority of typescript code that you work with this type annotation
is trying to tell typescript that we are only ever going to assign a value of type number to the variable
of apples if I ever tried to assign some other type of value besides a number all very quickly see an
error message.
So for example if I update the five right here to true which is a boolean I'll see an error right away
and it tells me that the type of true is not a sizable to a type of number.
Now this is also going to work if I tried to update the value assigned to that variable as well.
So if I update the cons keyword to let.
Which will allow me to reassign apples.
I can then reassign apples down here a value of 10 without any issue however if I tried to assign it
some other type of value like a string.
Once again I'll very quickly see an error and once again I see a message that says essentially you have
a string and you're trying to assign it to a variable that's supposed to be a number not for the rest
of this file.
I want to show you some reassignments all over the place.
So I'm going to continue using the left keyword even though in just about all the declarations we're
going to add here.
Usually I would be making use of the concert keyword instead type invitations like this right here can
be used with any type of value.
Remember we just spoke about some of the different types of values inside of typescript.
So we've got no strings billions no void no undefined and functions arrays and so on so with every one
of these different types of values we can use a type invitation let's write out a couple of examples
just to understand how type notation works with each of these different types.
All right.
So in every case we'll write outlet and then a variable name.
In this case I going to call my variable speed then a colon then the type of value.
In this case I going to say it's going to be a string and I'm going to assign it a value of fast like
so once again if I tried to assign this a value that is not a string.
So in this case maybe a number I'll see an error and that's pretty much it.
We're going to see that same pattern repeating every time we tried to assign it a different type of
value.
Let's take a look at a boolean as well so let's say maybe let has name is going to be a boolean of true.
How about no.
I'll make a variable called nothing much and I'll make that null and that's going to be assigned a value
of null.
Now this is kind of interesting right here it's our first example of where we have a value that has
a name identical to its type.
We're actually going to see that a couple of times in typescript.
That's something that's a little bit difficult to grasp.
Once you start getting into some more complicated topics we're gonna have several lectures later on
to understand when we're working with something that's a value.
And when we're working with something that is a type we could also try a new variable called something
like say nothing and make it undefined like so now we can also use type notation with every other type
of value as well like we just said.
So I can use this with an instance of a class or with a built in object or just about anything you can
imagine.
So let's say that we're looking at maybe a Date object.
I'll say I'll put a little section header here and I'll say built in objects so I can make a new variable
called now I'll say that's going to be a type date and then I'll make a new date and assign it on the
other side.
So in this case we're once again doing the same thing we're doing up here we're saying that we're going
to have a variable called now and we can only assign it a value of type date and then on the other side
we're going to assign it an instance of a date object.
All right.
So now we're kind of getting the idea here let's take a quick pause when we come back the next section
we'll take a look at some of the more complicated syntax that comes up when we start working with objects
arrays and functions.
So quick posit I'll see you in just a minute.