In this video I want to show you how we can still use arrays with multiple different types of values
inside them.
So let's look back over to our code editor and once again write out a quick example.
I gonna put in a new comment down here that says flexible types and then I'm going to declare a new
array called important dates like so.
So this is going to be an array that contains some different important date values maybe some of them
are going to be instances of date objects and maybe some of them are going to be strings that contain
like a date inside them.
So in other words I want to have two different types inside this array.
Some are going to be instances of data objects.
Some are going to be strings.
So to do so I could simply write out a new date and then like an actual string and here's a 20 30 maybe
10 10.
Like so if I now hover over important dates I'll see an update a little type invitation here.
So in this case.
Notice how little pipe operator right there.
We've spoken about that very briefly before.
That's kind of like an or statement for types.
So this can be read as saying this is going to be an array that contains elements that are a string
or a date.
So everything inside this array can be treated as a string or a date object.
Now it is important to understand this annotation right here.
If you ever happen to say only initialize the array with date objects and we hover over this once again
typescript now thinks that this array will only contain dates.
So if you wanted to specifically say that this array can contain multiple different types we would once
again have to add in a manual annotation here.
So I could put in a colon and then just like that syntax we saw in that tooltip we would say date or
string and then the square brackets right after.
And so now once again if I hover over important dates we've overridden type inference and said specifically
this array contain can contain either type of value.
So now right after that we could do important dates that push and add in a string.
Well let's do a really string.
Real string here.
So 20 30 10 10 and important dates dot push new date like so and as you might guess if we now try to
put in a new value that is just like a plane number we will see an error because that is neither a string
nor a Date object now as we mentioned before.
You will want to put in the sanitation if you ever do not initialize the array with a date literal and
a string literal.
Alternatively if you initialize it with an empty array you will want to have the annotation on there
as well.
If we don't put on that annotation with an empty array we're always going to end up with an array of
the any type which we always want to avoid as much as possible.
All right.
That's how we put in multiple different types of values into one single an array.
Let's take a quick pause right here and talk about where we are going to actually make use of these
typed arrays.
And as you might guess it's probably gonna be everywhere.
So quick pause and I'll see you in just a minute.