TanStackT
TanStack•2y ago•
1 reply
foolish-indigo

Declare type of useQuery with initialData

Hello!

I have a custom hook that is defined as follow:

export const useTodos = (): UseTodos => {
    return useQuery<Todo[]>({
      queryKey: 'todos',
      queryFn: fetchTodos,
      initialData: [],
    });
  }

type UseTodos = UseQueryResults<Todo[]>


Unfortunately, our linting rules require for all functions to have a type defined 🫠
When I use the UseQueryResults, this does not considers that initialData is being provided, hence when accessing
data
from the hook, this is defined as Todo[] | undefined.

Do you know how else I can define the hook type so that it knows data is always an array?
Thanks!
Was this page helpful?