- Now, the next modifier I wanna talk
about is the typeof modifier,
and it's actually different than
the type of modifier in JavaScript.
If you're used to JavaScript code,
you know that you can take any variable
and you can get the type of it by just saying,
typeof, all by the variable name,
and this will return to it, it's like object, string,
number, Boolean, whatever it is, but inside of TypeScript,
when you use this as a type definition,
it works a little bit differently.
Essentially, what it does is it tries to get the type
of something and it uses that as
whatever the type of this new thing is going to be.
In our case, you can see here that I have a person object,
which just has a name and an age.
Then, I have a people array
and I'm adding in some people to that array
and I'm also pushing a number to that array, and obviously,
I want to make sure that this array
can only have people inside of it.
So I could manually type this out by saying,
name, string, age is going to be number,
just like that, and now,
it's going to work where I'm going
to be getting an error array here for this.
I need to make sure that this is an array. There we go.
So as you can see,
I'm getting an error because I'm trying
to push two into here,
but this is a bit of a problem because let's say I want
to add additional properties to this user here.
Well, I would also need to make sure I add them
up in this type here as well.
That is not always super ideal.
So instead, we have this person up here.
I'm gonna make sure I just push them into this array.
We can just push 'em like that,
that works fine, and essentially,
I want the type of my people array to be whatever
the type of this person object is.
Well, I can just do that inside of JavaScript.
I can say here that I want to get
the type of, this person variable,
and the type of this person variable
is just going to be this type right here.
It's gonna be a name that's a string
and an age that's a number.
As you can see, if we hover over this,
that's exactly what it's getting from here.
Now, I want to make this an array,
so I can just put some parentheses around this,
say that this is the array syntax, and now,
if I add properties, for example,
to this person up here, such as isProgrammer,
and I make that a Boolean,
you can now see that I'm getting an error right here
'cause this is missing that isProgrammer property, and now,
I wanna add that in, you can see it's working fine,
and this should say an actual value right there,
there we go, and if I remove this again,
you'll see that I'm getting error
until I add it back in there.
So the really great thing is that my people array
is essentially depending on this person variable right here,
which is really nice.
So this is one place where you may want to use this.
Another place is if you have a function,
let's just say, we'll call it, "Say hi," there we go,
we'll give it a name, there's a string, console.log name,
there we go, so it's just gonna be saying "hi," whatever,
if I wanted to get the type of this,
I could just say, "typeof sayHi,"
and I could set that to a specific type,
I could say, like, "type FuncType is equal to,"
and now, if I hover over this,
you can see it gives me the type signature
of that exact function.
This is another really useful place
where you can use this type of function.
Now, the one thing to know about typeof is
that you can't do super complex stuff.
You can pretty much just pass it a variable and that's it.
Like, I can't call this sayHi function
and get the return value of that
and do kind of fancy stuff with that.
This doesn't work. This is gonna throw an error.
You can pretty much just pass it a variable
or a function name and that is about all you can do.
This is something that may seem kind of niche,
but as you start to get to some
of the more advanced use cases of TypeScript,
you're gonna be doing stuff like this all the time,
and this function use case is a really common example
of where you might want to do something like this.