query icon indicating copy to clipboard operation
query copied to clipboard

[Typescript] Unexpected type errors

Open vadzimmaisiuk opened this issue 6 years ago • 3 comments

CodeSandbox: https://codesandbox.io/s/romantic-tharp-kxg7n?file=/src/useQuery.ts

import {
  useQuery as useQueryBase,
  QueryOptions,
  QueryResult
} from "react-query";
import { useCallback } from "react";

export function useQuery<Data, Variables>(
  fetchData: (v: Variables) => Promise<Data>,
  variables: Variables,
  config?: QueryOptions<Data>
): QueryResult<Data> {
  const fetchDataEnhanced = useCallback((_, v: Variables) => fetchData(v), [
    fetchData
  ]);
  const result = useQueryBase(
    [fetchData.name, variables],
    fetchDataEnhanced,
    config
  );
  return result;
}

Even though this is a valid usage according to the docs: https://github.com/tannerlinsley/react-query#query-key-variables

vadzimmaisiuk avatar May 31 '20 13:05 vadzimmaisiuk

Your Variables doesn't have any form. Try to make them extend AnyVariables from react-query, that might solve your issue (I'm not sure as I have a little trouble reading all the overloads for useQuery myself). In any case, I'm pretty sure this is not an issue with react-query.

hankeypancake avatar Jun 02 '20 08:06 hankeypancake

export function useQuery<Data, Variables extends object> (
  fetchData: (v: Variables) => Promise<Data>,
  variables: Variables,
  config: QueryOptions<Data> = {
    retry: 2,
    cacheTime: 5 * 60 * 1000,
  },
): QueryResult<Data> {
  const fetchDataEnhanced = useCallback((_, v: Variables) => fetchData(v), [fetchData]);
  return useQueryBase([fetchData.name, variables], fetchDataEnhanced, config);
}

see extends object

jakwuh avatar Jun 07 '20 22:06 jakwuh

This issue has become stale. Please reopen a new issue with more/better information on how to replicate or solve the problem. Thanks!

tannerlinsley avatar Jun 22 '20 20:06 tannerlinsley