(v4.22.0
) How to use signal
with useQueries
? (signal
is of type any
)
#4785
-
Is there a way to integrate CodeSandbox: https://codesandbox.io/s/hungry-rui-utc9ms?file=/src/App.tsx It is very easy to use for useQuery({
queryKey: ["foo", todoId],
queryFn: async ({ signal }) => await fetchTodo(todoId, signal)
}); ..but useQueries({
queries: ["bar"].map((todoId) => ({
queryKey: ["foo", todoId],
// ❌ `signal` is of type `any` here
queryFn: async ({ signal }) => await fetchTodo(todoId, signal)
}))
}); Am I using it wrong or is this a bug in the types? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
good question. seems to be a shortcoming of the |
Beta Was this translation helpful? Give feedback.
-
Hi @bennettdams, afraid this is indeed a bug in the types. Short termI've added a workaround example to your sandbox:
// ✅ useQueries with signal explicitly typed
const res5 = useQueries({
queries: ["bar"].map((todoId) => ({
queryKey: ["foo", todoId],
queryFn: async ({ signal }: { signal?: AbortSignal }) =>
await fetchTodo(todoId, signal)
}))
});
Medium termThere's some work happening on the TS side that might unblock this so we can fix the bug (related to discussion here), but in the meantime the above should work for you / continue to be valid even after the fix. |
Beta Was this translation helpful? Give feedback.
Hi @bennettdams, afraid this is indeed a bug in the types.
Short term
I've added a workaround example to your sandbox:
res5
andres6
- https://codesandbox.io/s/lingering-firefly-mb8wgm?file=/src/App.tsxres5
(copied below) => you can get the types to work by providing the signal type explicitly.res6
=> you still get type checking, in the sense that if you give your signal the wrong type e.g.{ signal?: string }
, it will complai…