Checking Types with Assertion Functions
Here we have a couple of different types of users, all expressed using interfaces.
We have an
interface User, which is like the base interface, then we have an
AdminUser and a
AdminUser has a
"admin", the other one has a
0:00 In this exercise, we have a couple of different types of user all expressed using interfaces. We have an interface user, which is like the base interface. Then we have an admin user and a normal user. One admin user has a role. The other one has a role of normal.
0:15 We've got a function called assertUserIsAdmin. We're not returning anything from this function. We're throwing if the user is not an admin. What that means is that we're expecting it to throw. It's going to throw at runtime if the user is a normal user. In this example function here, we're basically taking in a normal user or an admin user.
0:38 We're asserting that the user is an admin. Then after we assert that, even though we're not returning anything from it -- const result is this -- then you notice that's just void. There's nothing there. Even though we're not producing any new variables we're expecting in this position, right here, that type of user is going to be an admin user.
1:00 You're going to need to look at something called an assertion function for this. You're going to be changing this function here to make sure that it is an assertion function. You'll be learning a bit of new TypeScript syntax here. Good luck.