Infer query options for useQueries
when using array function (map
) as input
#3425
-
Is there a good way to utilize type-safe query options when using an array as input for Here's a basic way to use const result1 = useQueries(
userIds.map((userId) => {
return {
queryKey: ["user", userId],
queryFn: () => queryFn(userId),
refetchInterval: 123
};
})
); In this case, You can circumvent that by giving const result2 = useQueries(
userIds.map<UseQueryOptions<QueryData>>((userId) => {
return {
queryKey: ["user", userId],
queryFn: () => queryFn(userId),
refetchInterval: 123
};
})
); Using the generic, the options are typed correctly. I created a small CodeSandbox that shows this behavior. I deliberately left https://codesandbox.io/s/priceless-matsumoto-0yiwc0?file=/src/App.tsx Result 1 (without generic, missing type for options): Result 2 (with generic, correct type for options): The example for dynamic parallel queries via ...or maybe const result3 = useQueries<QueryData[]>(
userIds.map((userId) => {
return {
queryKey: ["user", userId],
queryFn: () => queryFn(userId),
refetchInterval: 123
};
})
); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
despite the codesandbox, I'm not quite sure what the problem is.
now that type errors with:
which also looks correct. Just because you hover something and it shows |
Beta Was this translation helpful? Give feedback.
despite the codesandbox, I'm not quite sure what the problem is.
refetchInterval
can be anumber
, sorefetchInterval: 123
is correct, and you'll get no type error here. If I do:now that type errors with:
which also looks correct. Just because you hover something and it shows
any
doesn't mean there is an issue with the types.…