4

When working with the useQuery hook in typescript/react I get an error saying Argument of type '(param: Param) => Promise<any>' is not assignable to parameter of type 'QueryFunction<IResponsePlanets, QueryKey>'.

but keep in mind that onSuccess function is working here. even though I get an error

This is the interface for params

type Param = {
    pageParam?: undefined;
    queryKey: [string, { page: number }];
}

This is the async function

const fetchFunction = async (param: Param) => {
    const [key, { page }] = param.queryKey;
    const response = await fetch(`https://swapi.dev/api/planet/?page=${page}`);
    return response.json();
};

This is the useQuery hook

 const { status, error, data } = useQuery<IResponsePlanets, Error>(["planets", { page: 1 }], fetchFunction, {
     staleTime: 5000,
     onSuccess: () => {
         console.log('working👍')
     }
 };
1
  • please provide a reproduction (e.g. codesandbox), it's hard to say otherwise Commented Sep 25, 2021 at 10:17

1 Answer 1

2

The problem is in signature of your fetchFunction

const fetchFunction = async (param: Param) => {
                                    ^^^^^
    const [key, { page }] = param.queryKey;
    const response = await fetch(`https://swapi.dev/api/planet/?page=${page}`);
    return response.json();
};

The argument should be of type QueryFunctionContext<Param>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.