So arrays are supported and with numbers strings boolean objects and arrays we got the core values or
value types javascript knows supported in typescript as you will learn at the example of objects and
arrays typescript even offers way more functional he's there.
But that's a bit more advanced which is why we'll have a look at that later.
But even these core types with what you'll learn in the previous lectures are supported now typescript
all to adds a couple of new concepts new types which we don't know from vanilla javascript.
For example the tuple type.
Now you might note tuples from other programming languages.
Javascript does not have them.
This is a tuple for example and you would say well this is an array.
It is an array but it's a fixed length array and actually not just fixed length but all the fixed type.
Now where could this be handy.
Let's say here on our person we actually have another property roll which is an array with exactly two
elements where the first one is let's say a numeric identifier like two.
And the second one is a string identifier and a human readable description like author.
Now this should always have exactly two elements because a user or a person in this application which
we're writing let's say can only have one role and a role is only made up of two elements.
Of course we could have used an object here but for some reason we want to have an array with exactly
two elements where the first element always is the numeric identifier.
And the second element always is the string identifier or description or however you want to call it.
Now if we hover over this typescript actually infers this strange type which we haven't seen before.
Effectively this means typescript understands that we have an array which might hold even strings or
numbers.
This is a union type and we'll dive into those later.
The key takeaway is that typescript understands that this should be an array with these types of values.
The downside is that we could run this code though person role push admin.
Now for our application this might not make sense because we know we only need two elements still typescript
doesn't know that we only want two elements.
We would also be able to switch person role.
The second element with index one to be a number as well.
This would work because typescript only knows that role should be of type String or no array and therefore
assigning a number here to the second element and therefore replacing it with a number would be allowed
because we're just saying something about the types we generally can use in there.
We know we want to have exactly that structure two elements first element no second element string but
typescript doesn't do that well for such a scenario.
A tuple would be perfect we can tell typescript what role should be by explicitly setting the type of
role.
So here we also have an example where typescript doesn't fir a type but we explicitly want to override
it.
Now here we can therefore add an explicit object type by adding a colon after person then the curly
braces and then name should be a string.
Age should be a number and Hobbes should be a string array so all as inferred by typescript.
So for dad we wouldn't have required this explicit assignment.
And as you learn it would have been bad.
But now we have a scenario where the inference does not work in the way we want and we want to set roll
to be a tuple.
Now instead of setting a two string array or number array we're using the same type that was inferred
here earlier.
We can now use a special type.
We use square brackets again but now inside of the square brackets we add no and then a comma and then
string.
Now this marks a tuple type here a tuple is a special construct typescript understands in JavaScript
it will be a normal array but during development with typescript we'll get errors with code like this
here.
Now what do tuples do this year tells typescript.
I want to have a special array with exactly two elements because I have exactly two types in there and
the first element should be a number.
The second element should be a string.
Hence if we compiled this code here I get an error regarding 10 not being a sizable as a type here because
we want to have a string as a second value.
And if I identified 10 as a second value here then I obviously don't follow this rule.
Now if your comment is out and we compile this works that's a bit strange because why does pushing work
here.
Because we're all the saying that role should have exactly two elements to why can we push admin onto
the role array here push actually is an exception which is allowed and bowls so unfortunately types
who can't catch this error.
But at least it ensures that we're not assigning a wrong value here and outside of push.
We also get some support regarding the length.
If we set person to roll to a new value for example an empty array is not allowed one with exactly the
structure we defined up there is allowed if I added an extra element year then we would again get an
error.
So the length isn't forced.
If we assign it like this but not for pushing and so on.
This is something you have to be aware of but still getting this support and this support is pretty
nice and if you have a scenario where you need exactly x amount of values in an array and you know the
type of each value in advance then you might want to consider a tuple instead of an array to get even
more strictness into your apt to be even clearer about the type of data you're working with the type
of data you're expecting.