Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ interface NormalUser extends User {
role: "normal";
}

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

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

it("Should throw an error when it encounters a normal user", () => {
const user: NormalUser = {
Expand All @@ -37,7 +47,8 @@ it("Should assert that the type is an admin user after it has been validated", (
const example = (user: NormalUser | AdminUser) => {
/**
* The fix is to make assertUserIsAdmin a function,
* not an arrow function. Lord above.
* or to add the explicit type annotation back to the arrow function
* Lord above.
*/
assertUserIsAdmin(user);

Expand Down