Skip to content
Source
Sign inSign up
TanStack/query

queryKey is not inferred when using queryOptions #8428

Send to my agent
ClosedAlexMeah(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

v5.61.0 breaks queryKey inference, I think as a result of this change https://github.com/TanStack/query/commit/3fa4b7cce3ddd81326f5907d532c015fd335b56e

Example

import { queryOptions as queryOptions1 } from "react-query-5-60-6";
import { queryOptions as queryOptions2 } from "react-query-5-61-0";

queryOptions1({
  queryKey: ["test"] as const,
  queryFn: () => fetch("https://example.com"),
}).queryKey;
// ^? (property) queryKey: readonly ["test"] & { [dataTag...

queryOptions2({
  queryKey: ["test"] as const,
  queryFn: () => fetch("https://example.com"),
}).queryKey;
// ^? (property) queryKey: QueryKey & { [dataTag...

We use queryOptions to create query factories and have invalidation helpers that rely on the queryKey type.

Failing test: https://github.com/TanStack/query/commit/a77db3ed0cc54bf0b35415abd90e222a545198c3

Your minimal, reproducible example

https://stackblitz.com/edit/rq-querykey-infer?file=index.ts&view=editor

Steps to reproduce

/

Expected behavior

queryKey should be inferred when using queryOptions

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS Chrome

Tanstack Query adapter

react-query

TanStack Query version

5.61.0

TypeScript version

5.5.4

Additional context

No response

  1. Thanks, I think I broke this with:

    If you want, you can take a stab at fixing it while retaining the functionality of that PR ?

  2. TkDodo added labelstypes

  3. TkDodo added labelspackage: react-query

  4. Is the goal of #8317 to avoid having to specify all the generics?

    This works on v5.60.6 prior to #8317:

    function somethingWithQueryOptions<
      TQueryFnData extends unknown,
      TError,
      TData,
      TQueryKey extends QueryKey,
    >(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>) {
      return options.queryKey
    }
    
    const options = queryOptions({
      queryKey: ['key'] as const,
      queryFn: () => Promise.resolve({ a: 1 }),
      select: (v) => v.a,
    })
    
    somethingWithQueryOptions(options)

    or

    function somethingWithQueryOptions<TError, TQueryKey extends QueryKey>(
      options: UseQueryOptions<number, TError, number, TQueryKey>,
    ) {
      return options.queryKey
    }
    
    const options = queryOptions({
      queryKey: ['key'],
      queryFn: () => Promise.resolve(1),
    })
    
    somethingWithQueryOptions(options)
  5. TkDodo closed this

Sign in to join this conversation.