TypeScript: Custom useQuery hook v4 #3856
-
Hi, I have a custom In v3 it looked like this function useControlledQuery<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
queryKey: TQueryKey,
queryFn: QueryFunction<TQueryFnData, TQueryKey>,
options?: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
): UseQueryResult<TData, TError> {
const { reactQueryEnabled } = useSelector<RootState, GlobalState>((state) => state.global);
// If react-query is enabled, leave the options decide when the queries are enabled or disabled
const isEnabled = reactQueryEnabled ? options?.enabled : false;
return useQuery<TQueryFnData, TError, TData, TQueryKey>(queryKey, queryFn, { ...options, enabled: isEnabled });
} However, now in v4 I am not able to use the same hook because it shows The Best, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
can you provide a typescript playground that we can tinker with? /cc @mattpocock :) |
Beta Was this translation helpful? Give feedback.
-
I can confirm this is also happening for hooks autogenerated by useQuery<CurrentAccountOp, TError, TData>(
variables === undefined
? ['currentAccount']
: ['currentAccount', variables],
useApiRequester<CurrentAccountOp, CurrentAccountVariables>(
CurrentAccountDocument,
).bind(null, variables),
options, // <-- error here
); No overload matches this call.
Overload 1 of 6, '(queryKey: QueryKey, queryFn: QueryFunction<CurrentAccountOp, QueryKey>, options?: (Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | ... 1 more ... | "queryFn"> & { ...; }) | undefined): UseQueryResult<...>', gave the following error.
Argument of type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey> | undefined' is not assignable to parameter of type '(Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | "initialData" | "queryFn"> & { ...; }) | undefined'.
Type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>' is not assignable to type 'Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | "initialData" | "queryFn"> & { ...; }'.
Type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>' is not assignable to type '{ initialData?: (() => undefined) | undefined; }'.
Types of property 'initialData' are incompatible.
Type 'CurrentAccountOp | InitialDataFunction<CurrentAccountOp> | undefined' is not assignable to type '(() => undefined) | undefined'.
Type 'CurrentAccountOp' is not assignable to type '() => undefined'.
Type 'CurrentAccountOp' provides no match for the signature '(): undefined'.
Overload 2 of 6, '(queryKey: QueryKey, queryFn: QueryFunction<CurrentAccountOp, QueryKey>, options?: (Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | ... 1 more ... | "queryFn"> & { ...; }) | undefined): DefinedUseQueryResult<...>', gave the following error.
Argument of type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey> | undefined' is not assignable to parameter of type '(Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | "initialData" | "queryFn"> & { ...; }) | undefined'.
Type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>' is not assignable to type 'Omit<UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>, "queryKey" | "initialData" | "queryFn"> & { ...; }'.
Type 'UseQueryOptions<CurrentAccountOp, TError, TData, QueryKey>' is not assignable to type '{ initialData: CurrentAccountOp | (() => CurrentAccountOp); }'.
Types of property 'initialData' are incompatible.
Type 'CurrentAccountOp | InitialDataFunction<CurrentAccountOp> | undefined' is not assignable to type 'CurrentAccountOp | (() => CurrentAccountOp)'.
Type 'undefined' is not assignable to type 'CurrentAccountOp | (() => CurrentAccountOp)'.ts(2769) |
Beta Was this translation helpful? Give feedback.
can you provide a typescript playground that we can tinker with?
/cc @mattpocock :)