From 0754ee7b028d10bd48a5b52ea8a422a03aa7365a Mon Sep 17 00:00:00 2001 From: Ala Ben Aissia Date: Mon, 15 Sep 2025 10:55:28 +0100 Subject: [PATCH] another 14 solution --- .../14-typescripts-worst-error.solution.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/03-type-predicates-assertion-functions/14-typescripts-worst-error.solution.ts b/src/03-type-predicates-assertion-functions/14-typescripts-worst-error.solution.ts index b021c4c..6816eb3 100644 --- a/src/03-type-predicates-assertion-functions/14-typescripts-worst-error.solution.ts +++ b/src/03-type-predicates-assertion-functions/14-typescripts-worst-error.solution.ts @@ -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 = { @@ -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);