Now that we've got a better understanding of what a type is, let's dive a little bit deeper into the
subject and then discuss why we care about types at all.
All right.
So first off, a couple examples of some very basic types.
So any time that we are working with a value that has a type of string, we would expect to see a value
that look like these right here.
So if I have a value that looks like high there, that is a string.
If I see a value that looks like a string with no characters, that is also string and something that
says today's Monday.
Yep, that's a string as well.
Likewise, if I've got some really small decimal number, that's a number, a negative number has a
number and a really large number is a number as well.
And the reason I break out no right here is that in some other languages, there are actually subtypes
of numbers.
So for example, in other languages, you might have a type to represent numbers that have a decimal
or you might have another type to represent a number that is only an integer and therefore doesn't have
a decimal.
In JavaScript or me in typescript, we have nothing like that, a number is a number, regardless of
whether it is positive or negative, has a decimal, doesn't have a decimal there.
All numbers.
If we have a value of true or false, we would say that its type is Boolean and then remember, we are
not only restricted to these basic types right here, every value in typescript has a type.
So, for example, if we made a new date object, the resulting object from that would have a type of
date.
And then if we had an object that had an ID, a completed flag, a title flag, we would say that that
is an object that has type to do.
And this one only applies if we were working in a code base that had to find this interface right here.
Remember this interface to find a new type.
OK, so now we've seen some examples of different types.
Let's talk about how all these types kind of relate to each other.
So in the world of TypeScript, we have two different categories of types, we have primitive types
and object types.
Primitive types are all the very basic types that you might get.
So like no strings, boolean null, undefined and then Simbel and void are two others we'll talk about
later on.
The other category we have access to are something called object types, and these are essentially any
types that you and I create or any other types that are built into the language itself.
So, for example, all the different functions that you and I create are going to have their own type.
And those are a object type, any class you and I create or he's going to have its own type.
Same thing with arrays and objects as well.
Now, why is there the divide here, like why are there two categories, one called primitive types
and one called object types?
Well, the true answer to this, we won't quite see until later on.
But just as a quick preview, we say that there is a separate category called object types right here,
essentially because you and I can write some code that will essentially trick TypeScript into believing
that one value is a different type.
I know that sounds really confusing right now, but essentially we can kind of sometimes trick the typescript
compiler in a very good way.
We can do that with object types only.
We can not do that with primitive types.
So that's why we have these two separate categories.
Again, I know that's confusing to hear right now.
I just want to understand why you will sometimes see these broken out into the two separate categories.
OK, so now that we understand a little bit more about type's until the last big question here, why
do we care about Type's at all?
If I had to answer that, I would say that there are two big reasons.
The first one is that types are used by the types of compiler to analyze our code for errors.
So think about the definition that we just gave or types.
We just said that types are an easy way to refer to the different properties and functions that a value
has.
Just keep that in mind for a second while you keep in mind I'm going to flip back over my code editor
and take a look at the code that we just wrote a little bit ago.
So inside of here, we defined a new type.
We said that we had a new type inside of application called to do and a to do is only ever going to
have the properties ID title and completed.
Then down here on line 12, we said that the variable to do was going to referred to an object of type
to do so, that means that the object that this variable refers to is only ever going to have these
properties.
The typescript compiler can use that information.
So if we start to refer to some property on that, to do variable that doesn't exist, TypeScript is
going to throw in aretz.
TypeScript knows that this variable refers to an object that has these properties and nothing else.
So if we try to reference some other property, there must be something wrong with our code in.
TypeScript can discover that and tell us that in the form of an error.
So that's why we generally care about types.
The other big reason that we care about types.
Back over here, one more is that allows it allows other engineers to understand what values are flowing
around in our code base.
If you've ever worked on a large JavaScript project before, you might have ran into the scenario where
you open up some file and you see some function in there with some really unclear argument name like
maybe a function with an argument name of like the letter A and nothing else.
In that case, on a large project, it might be really hard for you to understand what that function
is doing.
If we instead tried to annotate the type of arguments being passed into a function by labeling the types,
then that at least gives other engineers some idea of what type of data is flowing around inside of
application.
And ultimately, it will make it slightly easier for other engineers to understand what our code is
doing.
So that's why we care about types, let's now take a quick pause right here.
When we come back, the next video, we're going to start to take a look at some more code snippets
to understand what's really going on.
So quick pause and I'll see you in just a minute.