Skip to content

Typing of Query<> should include query.options.enabled #4761

@nicolassanmar

Description

@nicolassanmar

Describe the bug

Context

I'm trying to do query invalidation based using predicates. For the particular use case, I would like to know if the query is enabled, since the typing of the queryKey may vary with it.
By using console log, I know that the queries in the query cache can have the query.options.enabled property, which I would like to use. Using query.isDisabled() is not equivalent, since it can return false even queries with enabled: false.

Issue

const queryClient = useQueryClient()
queryClient.invalidateQueries({
  predicate: (query) => {
    // query.options.enabled is not included in typing, but it is part of the object if we console log.
    return false
   },
})

Your minimal, reproducible example

https://stackblitz.com/edit/react-ts-dc5yox?file=App.tsx

Steps to reproduce

  1. Create a query, and call it using {enabled: false}
  2. Call the query client, and try doing query invalidation using predicate:
  3. console.log the queries, you will see that the one that has enabled: false will appear as having it on:
{
...
options: {
  ...
  enabled: false
  }
}
  1. Even though this happens, the typescript type says the {enabled? : boolean} property is not on the options object.

Expected behavior

I expected to have

query.options: typeof query.options & {enabled? : boolean}

as the type of the query options

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • Linux - Ubuntu
  • Firefox and Chrome

TanStack Query version

4.20.4

TypeScript version

4.9.3

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions