TanStackT
TanStack4y ago
7 replies
full-green

Proper types for custom query hook

Hello. I'm trying to add types to custom query hook but get an error. What do I do wrong?

Here is the code:

export const useFetchUserQuery = <TSelect = UserType>({
  ...options
}: UseQueryOptions<UsersFromApiType, ErrorType, TSelect> = {}) => {
  return useQuery<UsersFromApiType, ErrorType, TSelect>({
    queryKey: users.retrieve,
    queryFn: usersRetrieve,
    select: ({ oldKey, ...rest }) => ({ newKey: oldKey, ...rest }),
    ...options,
  });
};


Here is the error:
Diagnostics:
No overload matches this call.
  The last overload gave the following error.
    Argument of type '{ context?: Context<QueryClient | undefined> | undefined; enabled?: boolean | undefined; staleTime?: number | undefined; refetchInterval?: number | false | ((data: TSelect | undefined, query: Query<...>) => number | false) | undefined; ... 31 more ...; meta?: QueryMeta | undefined; }' is not assignable to parameter of type 'QueryKey'.
      Type '{ context?: Context<QueryClient | undefined> | undefined; enabled?: boolean | undefined; staleTime?: number | undefined; refetchInterval?: number | false | ((data: TSelect | undefined, query: Query<...>) => number | false) | undefined; ... 31 more ...; meta?: QueryMeta | undefined; }' is missing the following properties from type 'readonly unknown[]': length, concat, join, slice, and 19 more.
Was this page helpful?