Types issue with useQueries #3483
Send to my agentuseQueries type includes Promise in the inferred type of .data, which is not renderable as React.ReactNode with the new react 18 typings.
failing test on beta:
it('infers returned Promises', async () => {
const key1 = queryKey()
// @ts-expect-error (Page component is not rendered)
// eslint-disable-next-line
function Page() {
const result = useQueries({
queries: [
{ queryKey: key1, queryFn: () => Promise.resolve(1)}
]
})
expectType<number | undefined>(result[0].data)
}
})
fails with:
TS2345: Argument of type 'number | Promise<number> | undefined' is not assignable to parameter of type 'number | undefined'. Type 'Promise<number>' is not assignable to type 'number'.
@artysidorenko could you take a look at this please?
TkDodo added labelsbug
TkDodo added labelstypes
TkDodo added labelsv4
Btw, I noticed something else along the way. My understanding from reading your other issue is that you want to warn with a type error anytime
queryFnresolves to undefined?I think at the moment (
betabranch) it only works when it's synchronousundefinedbut notPromise<undefined>Errors as expected
// @ts-expect-error useQuery(key, () => undefined)This doesn't complain
useQuery(key, async () => undefined)Happy to look into it later unless that's intentional
Happy to look into it later unless that's intentional
oh yes good catch.
Promise<void>should also error on v4. Thank you 🙏TkDodo closed this
just to be sure, @artysidorenko, the
async () => undefinedissue wasn't fixed with #3493 right?from what I can see:
useQuery(key, () => undefined)errors, as expected ✅useQuery(key, () => undefined)no error ❌useQueries({ queries: [{ queryKey: key, queryFn: () => undefined }]})no error ❌useQueries({ queries: [{ queryKey: key, queryFn: async () => undefined }]})no error ❌
should I create a separate issue for that?
Indeed, it wasn't fixed in #3493 , planning to look into it next. If it helps from a tracking pov, can use a separate issue, sure.
follow up created: https://github.com/tannerlinsley/react-query/issues/3516
Sign in to join this conversation.
Heya, had a look, added a PR to fix. Wasn't so much react-18 related; previously it had been correctly getting the
Promise<string>=>stringinference, but got tripped up by this union added in a recent PR