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 type script along.
The type sanitation 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 I'm 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 folder, I'm going to make a new file called Variables Dotty'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 Appel's is going to be a number.
I'm going to set that to five like so.
So the colon and the word no, 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, I'll 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 assignable to a type of number.
Now, this is also going to work if I try to update the value assigned to that variable as well.
So if I update the keyword to let, which will allow me to reassign apples, I can then reassign apples
down here, a value of ten 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 no.
Now 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 key word, even though in just about all the declarations we're going
to add here, usually I would be making use of the const keyword instead.
Type annotations 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 booleans, no void, null, undefined and functions, arrays and so on.
So with every one of these different types of values, we can use a type annotation, let's write out
a couple of examples just to understand how type annotation works with each of these different types.
All right, so in every case, we'll right out let and then a variable name in this case, I'm going
to call my variable speed than a colon, then the type of value in this case, I'm 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 you there 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 his 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 no.
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 times in TypeScript, and it's something that's a little bit
difficult to grasp once you start getting into some more complicated topics.
We're going to 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.
Now, we can also use type annotation 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 working with 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 that we're kind of getting the idea here, let's take a quick pause.
When we come back in 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 a quick pause and I'll see you in just a minute.