TanStackT
TanStack2mo ago
12 replies
worthy-azure

Type error when disabling useLiveSuspenseQuery

I'm trying to conditionally enable/disable a simple query based on an external value like so:

export const useUser = (userId? : string) => {
  return useLiveSuspenseQuery((q) => {
    if (!userId) return undefined
    return q.from({ user: usersCollection }).where(({ user }) => eq(user.id, userId)).findOne()
  }, [userId])
}


However, returning undefined from the useLiveSuspenseQuery callback produces a type error:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(q: InitialQueryBuilder) => QueryBuilder<{ baseSchema: { user: any; }; schema: { user: any; }; fromSourceName: "user"; hasJoins: false; } & SingleResult> | undefined' is not assignable to parameter of type 'Collection<object, string | number, Record<string, any>, StandardSchemaV1<unknown, unknown>, object> & SingleResult'.
      Type '(q: InitialQueryBuilder) => QueryBuilder<{ baseSchema: { user: any; }; schema: { user: any; }; fromSourceName: "user"; hasJoins: false; } & SingleResult> | undefined' is not assignable to type 'Collection<object, string | number, Record<string, any>, StandardSchemaV1<unknown, unknown>, object>'.ts(2769)


When I change the hook to use useLiveQuery instead of useLiveSuspenseQuery, the type error goes away.

Is this intended behavior? If not, how might I able able to fix it? Thanks!
Was this page helpful?