Skip to content

Improve type safety for useAuth hook with ensureSignedIn#196

Merged
nicknisi merged 1 commit intomainfrom
nicknisi/auth-hook-types
Jan 31, 2025
Merged

Improve type safety for useAuth hook with ensureSignedIn#196
nicknisi merged 1 commit intomainfrom
nicknisi/auth-hook-types

Conversation

@nicknisi
Copy link
Member

This PR improves the TypeScript type safety of the useAuth hook by adding function overloads that guarantee user data is available when ensureSignedIn: true is used.

Changes

  • Added function overloads for useAuth to provide stricter types
  • When ensureSignedIn: true and loading: false, TypeScript now knows user is defined
  • No runtime behavior changes, purely type-level improvements

Example Usage

// With ensureSignedIn: true
const auth = useAuth({ ensureSignedIn: true });
if (!auth.loading) {
  console.log(auth.user.email); // TypeScript knows user exists
}

// Without ensureSignedIn
const auth = useAuth();
if (!auth.loading && auth.user) {
  console.log(auth.user?.email); // Need to check for user
}

Testing

  • No runtime tests needed as this is a type-level change
  • Verified TypeScript compiler accepts the new types
  • Verified existing usage patterns still work

@nicknisi nicknisi merged commit 3a62b12 into main Jan 31, 2025
4 checks passed
@nicknisi nicknisi deleted the nicknisi/auth-hook-types branch January 31, 2025 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants