Type Predicates and Assertion Functions 6 exercises
Problem

Avoiding TypeScript's Most Confusing Error

We have the assertUserIsAdmin function:


const assertUserIsAdmin = (
user: NormalUser | AdminUser,
): asserts user is AdminUser => {
if (user.role !== "admin") {
throw new Error("Not an admin user");
}
};

But down in the tests we have what I believe is TypeScript's wor

Loading exercise

Transcript

0:00 This is kind of a fun exercise. This is Typescript's worst error. In future Typescript versions, this may change. For now, this is for sure Typescript's most unreadable, horrible, horrible error.

0:14 I wanted to give this to you so that you can struggle with it for a brief moment and then look at the solution. The error here is that we're getting here, assertions require every name in their call target to be declared with an explicit type annotation.

0:30 This looks like pretty much exactly the same code as we had before, assertUserIsAdmin user, user normal user or admin user. Not getting an error here, so this looks fine, but this is erroring. Your job is to work out why the hell that code is erroring, but please don't spend too long here.