TanStackT
TanStack3y ago
4 replies
rubber-blue

Query Key Factory type not inferred for old data in queryClient.setQueryData

So I am playing around with the query key factory and looking for patterns to establish that I will use, and the first "roadblock" I hit is with queryClient.setQueryData.

const useFollowUser = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ walletAddress }: PutFollowUser["req"]) => {
      const userApi = new UserAPI();

      return userApi.followUser({ walletAddress });
    },
    onSuccess: (_, { walletAddress }) => {
      // oldFollowers does not have its type inferred as string[]
      queryClient.setQueryData(queries.user.followers().queryKey, (oldFollowers) => [...oldFollowers, walletAddress]);
    }
  });
};

const user = createQueryKeys("user", {
  followers: () => ({
    queryKey: ["user", "followers"],
    queryFn: () => {
      const userApi = new UserAPI();

      return userApi.getFollowers(); // this has the return type of string[]
    }
  }),
});


If I decide to give it
string[]
myself, I get a typescript error.
If I dont give it a type, I also get a typescript error on the spread
image.png
image.png
Was this page helpful?