For this I'm back in the general project set up.
We worked on before you find it attached empty.
Next H to metal fall empty apt.
Yes fall in in there.
I want to dive right in with intersection types intersection types allow us to combine our types.
Let's say we have a type admin which is an object type.
So again this is not creating object here.
We're defining a type.
So I'm just finding a type definition here and admin should have a name which is a string let's say
and an admin should also have privileges and that should be a String array.
For example now we could have created this with an interface.
But here I am doing it with a type and now I have another type general employee type let's say and there
I also have a name string.
I have no privileges but I have a start date and that could be of type date.
We haven't seen that before but date is a type that is supported by typescript based on the date object
that is built into javascript.
So now we want to create a new type here an elevated employee let's say which should be the combination
of these two types.
Of course we could created manually by simply defining an object type which has a name which has a start
date and which has privileges.
But if we have these two types already then we can all just combine this by saying admin and so we used
the M percent symbol here employee and now the result is a new object type which you must have both.
So if I have my employee one which should be of type elevated employee then I can store an object in
there which must have a name property which must have a privileged property
create server and which also must have a start date and with new data I create a new Date object which
simply is the current timestamp.
So this is now a simple way of combining two types.
No I will say that intersection types are closely related to interface inheritance because of course
we could have achieved the same year by using an interface admin and then an interface employee and
then we could also use an intersection type on these interfaces or we create a third interface elevated
employee which extends
employee and admin and therefore if I now comment this out we would have the exact same effect.
Now arguably that's a bit more code one reason why we maybe would prefer to use types here instead of
interfaces but the relation definitely is very close and you could absolutely use interfaces for this
example.
However it is worth noting that whilst intersection types can be especially useful when used in conjunction
with object types as we're doing it here you can use them with any types.
So if you had some other type calm buyable let's say and that's either a string or a number.
So we have a union type here with the string or number of base types and then we had a numeric type
here which is either a number or a boolean well then we could have our let's say universal type here
by intersecting combined table with numeric and in the end typescript then seized that universal of
courses of type.
No because that is the only intersection we have here.
But if you had more intersections then this would be the type that is assigned universals or that could
also be a union type which is the intersection of these two union types.
So the intersection operator can be used with any types and it then simply builds the intersection of
these types.
In the case of a union type that is basically the types they have in common.
In the case of object types it's simply the combination of these object properties so to say so these
are intersection types can sometimes be useful.
You will not use them all the time but you definitely can encounter situations where you can express
something in a simpler or shorter way by using intersection types.
Now just to prove that this also all works.
Let me open up a new tab here in my terminal and run TSC dash W to start to watch mode so that I don't
just have to ongoing live server but I actually also build my code now for reload once here we see this
runs without errors and it also compiles without errors.