- Now the next basic type I want you to learn
is going to be the array type.
So in order to understand how the array type works,
let's just take a variable and assign it to an array
and see what TypeScript infers the type as.
So I can say const a equals,
let's do an array of one, two, three.
So this is just three different numbers.
I hover over this, you can see that my type
has been set to number with this square brackets at the end.
And that's just a noting that this is a number array.
So an array of numbers.
So if I wanted to manually type this,
I could say that this is going to be
a number array just like that.
So now if I were to move the default value,
and I said that A is equal to for example, one, two, three
you can see that works fine.
Also I need to make sure I give this
some type of default value,
otherwise it's gonna gimme an error.
'Cause undefined is not a number array.
Also, I should probably use let here instead
just to make sure all that's working.
The really nice thing about manually defining these,
is if I come in here with const,
and I give this a value of like let's say one, two, three
like we had before,
and then I can do inside of here, is I can say dot push.
And I say I wanna push a new value to the end of this.
TypeScript is smart enough to know,
"Okay, this is an array of numbers,
so I can only add numbers to be added onto this."
So I can only put numbers in here.
If I were to try to put a string,
you can see I'm going to get an error
because I'm trying to add a string to an array of numbers.
So this again, is where the power of TypeScript
really explodes because by typing just one thing,
and I don't even need to include the type
'cause it's already inferred,
you can see that I'm getting this awesome auto complete
and this error protection in my code.
And this code is just plain JavaScript.
I haven't even written any TypeScript
and I'm getting all of these awesome errors
just by all of this inference.
So if you want to type an array,
just make sure that you're coming in here
and you're doing whatever the type is.
For example, a number
followed by these square bracket syntax
to denote that this is specifically an array of numbers
and not just a single number.
Now another thing to note is I have defined this const a
as one, two, three.
So you may think if it's following the literal type practice
that this a would be typed as an array
of values one, two, and three.
I could manually type that out if I wanted to,
by just saying one, two, three, like that.
And now I'm saying that a
must be the values of one, two and three,
which means I can't push anything at all to it.
If I try to push another number,
for example, I push the value of four into here,
you can see that I'm getting an error
because four is not a value of three, one, or two.
So the reason that I don't get this actual
like explicit literal type when I define my array like this
is because arrays just like objects
they have a pass by reference as opposed to pass by value,
which essentially means that I can call this push method
which actually modifies my array
without actually redefining it.
Just like I would have to do with saying something like,
a equals one, two, three, four.
I can do the exact same thing as this code
just by calling a.push.
And that doesn't actually redefine the const variable.
So the fact that this uses references instead of values
and the fact you can modify a
without actually redefining it,
TypeScript says that this knows that it's a number array
and not explicitly the values of one, two, and three.
Which is really great because
if it assumed the values were one, two, and three,
it would almost always be wrong, since you're always
pushing and removing elements from arrays.
And the great thing is this works
with all the different types you could possibly think of.
For example, if we had an array of strings,
you can see I get a string array here, here and so on.
So all your different primitive types,
and you can combine this together.
For example, if I were to put dates inside of here,
or if I just say, this is a new date,
you can now see I have an array of dates.
So specifically I could say that this is a date array
just like that, and that's going to work fine.
Now in this case,
a date is essentially an object type inside a TypeScript,
and we're gonna be covering that in a little bit.
So it's not super important to know,
but just know that you can use things
that aren't primitives all over the place.
So everywhere you could normally use a primitive
you can use other things like dates and so on.