Skip to content
Source
Sign inSign up
TanStack/query

False positive in @tanstack/query/exhaustive-deps when combining query key variable with function invocation #9643

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

Describe the bug

In https://github.com/TanStack/query/blob/main/packages/eslint-plugin-query/src/rules/exhaustive-deps/exhaustive-deps.rule.ts#L83 an identifier is only de-referenced if the target is an array.

This was written when the rule wasn't able to handle key factories (see TODO near the original commit: https://github.com/TanStack/query/commit/82adc95c2403a2f63aa9554223f0140f02497111#diff-1db59b20778c62ed3511c0664212006c2217e7c1ca7d07b79062b6e93fa3fc8b )

I wold think identifiers can always be de-referenced?

Anyway, this causes code like this

function queryKeyFactory(dependency: string) {
  return ["my-key", dependency];
}

function example(dependency: string) {
  const queryKey = queryKeyFactory(dependency);
  return queryOptions({
    queryKey,
    queryFn: () => Promise.resolve(dependency),
  })
}

to fail with "error The following dependencies are missing in your queryKey: dependency"

Note that there's a similar issue for as-expression in the same file in line 72.

Your minimal, reproducible example

https://eslint-online-playground.netlify.app/#eNp9UstuwjAQ/BXLlwSJhFPVioqqUisuPbQ9Yw6Rs0GGxE5tBxGh/HvXD4XQCk55eGZ2ZtZnajRfwKlo2hryvaFLKppWaUvO5KcD3X+2VihpyEAqrRrC6KstpLEFPyw0FNxmHsXoM5NMVp3kDh6oH9CvEaF0n5bQgixB8n5GzkwSosF2WpINo02fHQAF5uQC2qLa4ATh5L2MutHofz2OHu04lqzuOUDx0cA0Y+qVyEidTz7XcknSGVm9kC+sQRjINRhVH6+seMIwQ+90TsHUQtocjVVi96fZEioh4c0fXZoNjEVg+EYjoa27nZDfzkfAJpclBFIWIGEZCTJjcTio6Gr/HAemzuUmZMvzfCIezZpNUtWFxf1y1TQuXJlsfbYtk649TNfi7GLnboySmMw3x6jtW2B0iW+NKrsa3z2N0bEkAcYBYtPT2+StZ1zpqPCQPz3kj4z6TkeZ4/tNpVjf3v9mFBOAsdGBA4TzG4d3C73meENux8MvUHEhcw==

Steps to reproduce

  1. Open ESLint Playground
  2. Observe error message in problems tab
  3. (optional) inline either queryKey variable or queryKeyFactory function to see error disappear

Expected behavior

As a developer I would like to be able to combine variables with functions for query keys without ESLint errors.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Linux
  • Node: v22.19.0

Tanstack Query adapter

vanilla

TanStack Query version

5.83.1

TypeScript version

No response

Additional context

No response

  1. TkDodocommented

    would you like to contribute this improvement ?

  2. TkDodo added labelspackage: eslint-plugin-query

  3. I submitted a PR.

    I wasn't sure if variable and type assertion de-referencing ought to be integrated into ASTUtils.getNestedIdentifiers, so I left it separate.

  4. TkDodo closed this

  5. I'm still getting this error with my code, not sure if I should open a new issue or we reopen this one?

    /Users/blake/Projects/Exceptionless/Exceptionless/src/Exceptionless.Web/ClientApp/src/lib/features/users/api.svelte.ts 108:9 error The following dependencies are missing in your queryKey: request.params.limit @tanstack/query/exhaustive-deps

            queryKey: [...queryKeys.organization(request.route.organizationId), { params: request.params }]

    https://github.com/exceptionless/Exceptionless/blob/main/src/Exceptionless.Web/ClientApp/src/lib/features/users/api.svelte.ts

Sign in to join this conversation.