Now besides objects we also have arrays in JavaScript very important type of data arrays are created
like this in javascript.
As you know and you can store any data in their numbers strings boolean as objects of a race you can
have nested Arrays after all and you can also mix data.
You can have an array with strings and numbers mixed and types with all the supports arrays any javascript
array is supported and the types of the array can be flexible or strict.
Now let's have a look at that in typescript for that here in Apt.
Yes.
Let's say our person also has a hobbyist key.
But of course we could also have a variable or a constant hobbies outside of the object.
So using arrays is exactly the same inside of objects and outside of them.
So here we have hobbies and hobbies could be sports and cooking let's say.
So I have two elements in there and these elements as you can tell are strings.
Now if you hover over a is typescript correctly detected that this of type String array.
Now that's a syntax you haven't seen before but this is how typescript describes an array of data.
You have to square brackets and in front of that the type of data which is stored in there.
Now this is the type type inferred it sorted here we have an array and that we only have strings in
there.
So it inferred that Hobbes probably should be the array of strings and indeed for Hobbes that makes
a lot of sense.
Of course you can also explicitly set the type of a variable if we would add a new variable favorite
activities.
And initially that should be empty then we might want to explicitly set the type of data stored in there.
Now let's say that should be an array of strings.
Well then we can just repeat what we just saw type string and then square brackets thereafter and this
tells typescript what we store in here is not just a single string.
It's an array of strings.
And indeed if I try to store just sports and there I get an error I get an error because that here is
a single string and not an array of strings.
I don't get an error of course if I wrapped as into square brackets and therefore effectively I create
an array.
I again do you get an error though if I add a number let's say because we defined this to be an array
of strings.
Now here we have a mixed array.
It's an array of strings and numbers.
So that does not work and is not supported here.
If we would want to support such a mixed array one solution would be to use any here.
D any type is a special type and typescript which will have a closer look later which basically means
do whatever you want.
It's of course a type you don't want to use too often because you'll lose the benefits typescript gives
you your back and JavaScript the world where you also can use any value anywhere.
So any is really flexible but the flexibility comes at the price of basically giving up all benefits
types good offers.
So hero when I go back to string because a re don't want to have a number in there and hence we can
remove the number and we're good erase all that give us a great example.
Actually just like objects and their properties.
How dynamic type scripts type inference is if we add a for loop here at the bottom and we want to go
through all the hobbies of person.
We can of course use a traditional for loop with for const hobby of person dog hobbies.
So accessing the hobbies property and going through all the hobby stair storing each hobby for each
iteration in this constant.
And then we can console log hobby here.
Now that's not too fancy.
That's something we know from javascript right.
If I now compiled is apt yes file down their we get the name from this line and they're after the two
hobbies being printed but what's actually interesting here is that on hobby we can access anything we
can access on any string for example to upper case.
I get this all to completion and typescript does not complain.
Why does it not complain because it knows that Hobbes is of type String array.
So when I access persons that Hobbes type scripts inference detects that Hobbes will be an array of
strings.
So a hobby on the other end is correctly identified as being a string because since we go through an
array of strings Well the individual values have to be just strings and therefore typescript offers
us great support down there and allows us to do anything with hobby that can be done with a string because
it knows with certainty that hobby will be a string because of the types we setup up there.
And that's really flexible in of course a great feature which makes writing code a lot easier and way
more flexible and secure.
For example we would get an error here if it would try to access hobby dot map.
If I thought this would be an array the map method is available on arrays but not on strings and here
correctly I get an error.
That map does not exist on type string because it doesn't.
So here we get an error and that's good that we get it because this would be wrong.
And here we see typescript inference really really shine.