Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useQueries type doesn't support a type narrowed onError #4767

Closed
paul-sachs opened this issue Jan 5, 2023 · 2 comments
Closed

useQueries type doesn't support a type narrowed onError #4767

paul-sachs opened this issue Jan 5, 2023 · 2 comments

Comments

@paul-sachs
Copy link
Contributor

paul-sachs commented Jan 5, 2023

Describe the bug

We currently use a library to build a series of query options with narrowed error types (because we know exactly what error should result from the queryFn. useQuery works fine with these query options but useQueries does not.

This may be related to #3991 but couldn't find a related issue

Your minimal, reproducible example

https://codesandbox.io/s/silly-sanderson-e95c55?file=/src/App.tsx

Steps to reproduce

const queryOptions = {
  queryKey: ["hello"],
  onError: (error: Error) => {
    console.error(error.message);
  }
};
const { error } = useQuery(queryOptions);
const [query] = useQueries({
  queries: [queryOptions]
});

Expected behavior

As a user, I expected useQueries to behave similarly to useQuery and accept the same queryOptions.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Doesn't make a difference

TanStack Query version

4.20.9

TypeScript version

4.9.4

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented Feb 14, 2023

it's because Error needs to be of type unknown:

https://codesandbox.io/s/ecstatic-euclid-0936xe?file=/src/App.tsx

The inference trick to provide a different type for Error via onError only works when inlined.

FYI, in v5, we have moved away from unknown and made Error the default type, and we're also going to provide a way to override the Error type globally.

Sneak peak (v5 is not yet available): https://tanstack.com/query/v5/docs/react/guides/migrating-to-v5#typescript-error-is-now-the-default-type-for-errors-instead-of-unknown

@TkDodo TkDodo closed this as completed Feb 14, 2023
@paul-sachs
Copy link
Contributor Author

@TkDodo Yeah i expected it was something like that. Alright, thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants