T
TanStack•14mo ago
sensitive-blue

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[]>
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!
1 Reply
correct-apricot
correct-apricot•14mo ago
do yourself a favour and turn off that lint rule 😂

Did you find this page helpful?