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

Tanstack Query: Use queryOptions type helper instead of UseQueryResult #1788

Open
Arnagos opened this issue Jan 6, 2025 · 1 comment
Open
Labels
enhancement New feature or request tanstack-query TanStack Query related issue

Comments

@Arnagos
Copy link

Arnagos commented Jan 6, 2025

Tanstack Query recommends using the queryOptions type helper instead of manually typing query results. This is now their recommended standard.

Not using this helper has already led to some problems because QueryOptions and SuspenseQueryOptions are now incompatible. This means that generated getUse*SuspenseQueryOptions() are not usable in useSuspenseQuery / useSuspenseQueries in the current version.

Type 'UseSuspenseQueryOptions<ValueType, ErrorType, ValueType, QueryKey>' is not assignable to type 'UseSuspenseQueryOptions<ValueType, unknown, ValueType, QueryKey>'.
  Types of property 'refetchInterval' are incompatible.
    Type 'number | false | ((query: Query<ValueType, ErrorType, ValueType, QueryKey>) => number | false | undefined) | undefined' is not assignable to type 'number | false | ((query: Query<ValueType, unknown, ValueType, QueryKey>) => number | false | undefined) | undefined'.
      Type '(query: Query<ValueType, ErrorType, ValueType, QueryKey>) => number | false | undefined' is not assignable to type 'number | false | ((query: Query<ValueType, unknown, ValueType, QueryKey>) => number | false | undefined) | undefined'.
        Type '(query: Query<ValueType, ErrorType, ValueType, QueryKey>) => number | false | undefined' is not assignable to type '(query: Query<ValueType, unknown, ValueType, QueryKey>) => number | false | undefined'.
          Types of parameters 'query' and 'query' are incompatible.
            Type 'Query<ValueType, unknown, ValueType, QueryKey>' is not assignable to type 'Query<ValueType, ErrorType, ValueType, QueryKey>'.
              Types of property 'state' are incompatible.
                Type 'QueryState<ValueType, unknown>' is not assignable to type 'QueryState<ValueType, ErrorType>'.
                  Type 'unknown' is not assignable to type 'ErrorType'.typescript(2322)

Minimal, reproducible example

https://codesandbox.io/p/devbox/rough-brook-drv2hx

Steps to reproduce

const [{ error, data }] = useSuspenseQueries({
  queries: [getSuspenseQueryOptions()],
});

function getSuspenseQueryOptions(): UseSuspenseQueryOptions<ValueType,  ErrorType> {
  return {};
}

type ValueType = { some: string; value: string };
type ErrorType = { error: unknown };
@Arnagos Arnagos changed the title Use queryOptions type helper instead of UseQueryResult Tanstack Query: Use queryOptions type helper instead of UseQueryResult Jan 6, 2025
@melloware melloware added the tanstack-query TanStack Query related issue label Jan 6, 2025
@melloware
Copy link
Collaborator

This seems like a great improvement! PR is definitely welcome.

@anymaniax anymaniax added the enhancement New feature or request label Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tanstack-query TanStack Query related issue
Projects
None yet
Development

No branches or pull requests

3 participants