T
TanStack•2y ago
flat-fuchsia

Strongly typing useQueries

Hi I am trying to use useQueries, and I have doubt with Typescript, I am trying to call multiple fetch functions in useQueries and it returns an array of combined types. I was wondering is there any way to strongly type the array with query v5: in the given code, is there any way to return the types like [string, date, number] considering those functions returns these types correspondingly?
const { data, isSuccess, isError } = useQueries({
queries:
[
{
queryKey: ['address'],
queryFn: fetchAddress(),
},
{
queryKey: ['date'],
queryFn: fetchDate(),
},
{
queryKey: ['registrationNumber'],
queryFn: fetchRegNo(),
},
],
combine: (results) => ({
data: results.map((result) => result.data),
pending: results.some((result) => result.isPending),
isLoading: results.some((result) => result.isLoading),
isSuccess: results.every((result) => result.isSuccess),
isError: results.some((result) => result.isError),
}),
});
const { data, isSuccess, isError } = useQueries({
queries:
[
{
queryKey: ['address'],
queryFn: fetchAddress(),
},
{
queryKey: ['date'],
queryFn: fetchDate(),
},
{
queryKey: ['registrationNumber'],
queryFn: fetchRegNo(),
},
],
combine: (results) => ({
data: results.map((result) => result.data),
pending: results.some((result) => result.isPending),
isLoading: results.some((result) => result.isLoading),
isSuccess: results.every((result) => result.isSuccess),
isError: results.some((result) => result.isError),
}),
});
10 Replies
genetic-orange
genetic-orange•2y ago
I'm actually wondering the same thing. When using useQuery my result is inferred but useQueries causes eslint to complain of "Missing return type on function". I also notice signal has type any when used in the queryFn, not AbortSignal. I currently have my queryFn typed myself using:
queryFn: ({ signal }: QueryFunctionContext): Promise<PartDetails> => partDetails.get({ id }, signal)
queryFn: ({ signal }: QueryFunctionContext): Promise<PartDetails> => partDetails.get({ id }, signal)
Is this the expected solution? I know the typescript interfaces for useQueries is annoyingly deep and complex.
typical-coral
typical-coral•2y ago
Show a typescript playground please
genetic-orange
genetic-orange•2y ago
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
typical-coral
typical-coral•2y ago
@blackfish in combine, your results are a tuple:
[UseQueryResult<string, Error>, UseQueryResult<Date, Error>, UseQueryResult<number, Error>]
[UseQueryResult<string, Error>, UseQueryResult<Date, Error>, UseQueryResult<number, Error>]
but then you call .map on them, and mapping on a tuple in typescript will make it an array. Keeping the tuple type on .map only works with homogenous tuples. see: https://github.com/microsoft/TypeScript/pull/11252
GitHub
Add overloads for 'map' on tuple types by DanielRosenwasser · Pull ...
This PR fixes #6574. Sometimes users have the expectation that calling map on a homogeneous tuple will create a tuple type of the same size, but with the element types appropriately mapped on. For ...
typical-coral
typical-coral•2y ago
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
genetic-orange
genetic-orange•2y ago
Sure, I'll open an issue now. Should I mention the eslint error too or create a codesandbox of that? Or a separate issue?
typical-coral
typical-coral•2y ago
It's a TS issue, not eslint
genetic-orange
genetic-orange•2y ago
Well I know that, but I wasn't sure if the inferred return was related to the incorrect ctx type. I'll just double check after this fix is released
flat-fuchsia
flat-fuchsiaOP•2y ago
Oh okay. I got it. Thanks for the info.

Did you find this page helpful?