In this video, we're going to right out just a little bit of code to get a better idea of what's going
on with these types.
Now, this first little bit of code that we're going to right around type's is going to be really simple
and straightforward just to kind of hammer home some of the concepts that we've been talking about.
But on some of the other examples or other topics we're going to discuss very shortly, we're going
to read out much more complete code snippets.
So anyways, let's get started.
I'm going to begin by flipping on over to my code editor.
I'm still inside of my fetch JSON directory.
I'm going to change out into my workspace folder and then inside of here I'm going to make a new project
directory to hold all these different code snippets we're going to put together as we're going through
some basic features of TypeScript.
So I'm going to make a new directory here called Features.
I'll then change into that directory and then I'll open up my code editor inside there.
Once my editors open and then going to make a new file and I'm just going to call it type.
So like I said, I just want to write out one or two quick snippets of code just to get a better idea
of what's going on.
So I want you to remember that the entire idea behind this type system is that it is an easy way for
us to refer to the different properties and functions that a value has.
In addition, every value, everything that we can assign to a variable has a type.
So to get started, I'm going to flip back over my code editor and I want to just try creating a variable
that's going to point to an instance of a date object.
So I'll say Kohn's today is going to be a new date like so.
If I hover over that variable, my editor will then tell me the type of value that this variable is
pointing at.
So this is saying that the today variable is pointing out an object of type data.
TypeScript can now use that information to decide exactly what we can do with that variable.
So, for example, if I put down today and then I dot, I see my autocomplete pop up right here, this
is listing out all the different functions and properties that a date object has.
TypeScript only knows to show this information right here because it has an internal definition of exactly
what a date is.
In other words, TypeScript knows the properties and methods that a date has so it can print them up
in this list right here.
And I can easily select one and call it like so.
And then, as we saw just a moment ago, because typescript knows what methods a date has and what properties
it has, if we tried to reference one that doesn't exist, we will very quickly get an error message.
So that's the whole point of type's.
It is a shortcut to say here are the different properties and methods that this value right here has.
OK, so that's an example of looking at the type that a date object brings up.
Let's now try creating and explain JavaScript object.
So I'll say something like consed person.
Is going to be an object like so, and I'll give this person how about like just an age of 20 if I now
hover over that person variable, I'll see a person or a person excuse me.
And then right after that, in this case, I don't get a nice name for the type because I've not actually
given this object right here.
A name is shortened name.
So we can kind of see our definition, that of a type kind of holds up here.
If we don't have a shortened definition of what this object is, then TypeScript is just going to default
to printing out all the different properties and methods that the object has.
So in this case, it has just an age that is a No.
And once again, if we tried to reference some property on that variable that doesn't actually exist,
we're going to get an error message.
OK, just one more quick example here, so down here, I'm going to try to create a class, I'll call
it color, I'm not going to give it any implementation.
Now, even if you don't know what a class is, that's totally fine.
We're going to go into great detail on classes later on.
I can now use this class to make a new instance of a color Salceda like, say, consed red is new color
like so.
And now, just as we've seen twice before now, if I hover over read, TypeScript knows that this variable
is pointing at an object that is of type color.
That means that this variable right here has access to all the properties and all the methods that a
color object has.
In this case, none, because we haven't actually implemented anything here.
And we can see that if we write out a red and then like so there are no properties associated with this
class of color.
So we don't get any autocomplete whatsoever.
If I try to reference anything as usual, I'll get a very quick error message here as well.
OK, so that's just a couple of quick example, examples of types, so let's take a quick pause right
here.
We're going to wrap up this idea of type's with a quick discussion, really quick, I promise, on where
we're going to use type's in a normal project.
So quick pause and I'll see you in just a minute.