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 anytime 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 hi there that is a string.
If I see a value that looks like a string with no characters that is also string and something it says
today's Monday yup 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 breakout number 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 see 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 they're 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 I.D. 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 defined
this interface right here.
Remember this interface defined a new type okay.
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 a 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 symbol 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 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 is going to have its own type.
Same thing with arrays and objects as well now why is there that 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 you understand why you will sometimes see these broken out into the two separate categories
OK.
Now that we understand a little bit more about types onto the last big question here why do we care
about types 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 for types.
We just said that types are an easy way to refer to the different properties and functions that a value
has to just keep that in mind for a second while you keep in mind I'm gonna 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 our application called to do and a to do is only ever going to
have the properties I.D. title and completed then down here on line twelve.
We said that the variable to do was going to refer 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 an error at US 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.
That's why we generally care about types.
The other big reason that we care about types back over here one Mark 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 in 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 gonna 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.