Skip to content

Commit

Permalink
fixed guard issue with undefined argument
Browse files Browse the repository at this point in the history
  • Loading branch information
LCcodder committed Nov 3, 2024
1 parent b62350d commit 9ae0968
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/functions/predicatesAndTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function generateConditionalGuard<T>(callback: (entity: T) => boolean):
export function generateGuard<T>(prop: keyof T, propPrimitive: string | symbol):
(checkingVariable: unknown) => checkingVariable is T {
return (checkingVariable: unknown): checkingVariable is T => {
return typeof (checkingVariable as Required<T>)[prop as keyof T] === propPrimitive;
return Boolean(checkingVariable) && typeof (checkingVariable as Required<T>)[prop as keyof T] === propPrimitive;
}
}

Expand Down
4 changes: 4 additions & 0 deletions tests/functions/predicatesAndTyping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ describe("generateGuard function", () => {
expect(guard(testUser)).toEqual(false)
})

test("Testing for guard failure (with undefined)", () => {
expect(guard(undefined)).toEqual(false)
})

test("Testing for guard success", () => {
const testUser = {
id: 123,
Expand Down

0 comments on commit 9ae0968

Please sign in to comment.