Intersection Types

These allow us to combine other types.

Screenshot 2022-10-30 at 9.18.09 PM.png

Notice: Here in line 22 - 25 the intersection is done based on the common type. Which is name in this case.

More on Type Guards

Screenshot 2022-10-30 at 9.38.20 PM.png

Note: Line 28 is a type guard at the runtime. There can be other type guards as well:

Screenshot 2022-10-30 at 9.37.56 PM.png

Screenshot 2022-10-30 at 9.39.44 PM.png

Here, you can see it is giving me an error. This is because it is not sure if privileges is present in the UnknownEmployee or not since we used a union type. Also we c eannot use the traditional typeof type guard here since the type will be object which is very generic. Also, we can’t even do this:

Screenshot 2022-10-30 at 10.03.50 PM.png

Since TS does not allow this. So, we can use the ‘in’ keywork provided by JS:

Screenshot 2022-10-30 at 10.08.31 PM.png

Notice: Line 38.

Similarly we can do:

Screenshot 2022-10-30 at 10.09.48 PM.png

Output:

Screenshot 2022-10-30 at 10.10.36 PM.png

We can also use instanceOf type guard like:

Screenshot 2022-10-30 at 10.13.57 PM.png