Declare Assertion Functions Properly to Avoid Confusing Errors
The solution here is that we have to use the function
declaration instead of using const
and an arrow function.
Change the declaration, and all of a sudden our error goes away.
function assertUserIsAdmin( user: NormalUser | AdminUser,): asserts user is AdminUser { if (user.r
Transcript
0:00 The solution here is the difference between this one and the one we did before is we've declared this with a const, whereas we declared the other one with a function. If you use a function, it suddenly starts working. Oh God, why on earth is it doing that?
0:17 I actually don't know why that error is thrown. I'd be really interested to talk to someone from the TypeScript team about this. Now it suddenly works and everything is gravy.
0:28 Make sure you declare your assertion functions with the function keyword, or if you're in a class, make sure you're not using arrow functions for them.