Skip to content
Source
Sign inSign up
TanStack/query

useMutationState filter predicate variables no longer working with typescript #8395

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

Describe the bug

after doing a "pnpm up" on my project I noticed new typescript errors

"@tanstack/react-query": "^5.59.19" updated to "@tanstack/react-query": "^5.62.2"

It will say "thing does not exist on type {}"

Your minimal, reproducible example

https://codesandbox.io/p/devbox/relaxed-scooby-9rgqft?workspaceId=ws_VPZaJaqxqdr5erejxx17SR

Steps to reproduce

Look in the codesandbox on line 43

Expected behavior

No typescript error, since there were none in the previous react query version

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

kubuntu chromium

Tanstack Query adapter

react-query

TanStack Query version

5.62.2

TypeScript version

5.7.2

Additional context

No response

  1. I have the same in a useMutationState() but also in a useQuery()'s placeholderData(). See below

        queryKey: ['campaigns', 'list', filters], // filters is type ==> Record<string, string[]>
    
        placeholderData: (previousData, query) => {
          const queryKeyFilters = query?.queryKey.at(2);
    
          if (
            queryKeyFilters === undefined ||
            typeof queryKeyFilters === 'string' ||
            // @ts-expect-error react-query types have changed to {}
            queryKeyFilters.windfarmId === undefined
          ) {
            return undefined;
          }
    
          // @ts-expect-error react-query types have changed to {}
          return queryKeyFilters.windfarmId.includes(windfarmId)
            ? previousData
            : undefined;
        },
  2. TkDodo added labelstypes

  3. The mutation passed to useMutationState is of type Mutation<unknown, Error, unknown, unknown>, and I don't think it has ever been anything. The issue title seems to imply that it did work sometime in the past? Can you create a reproduction with a version where this worked?

    The thing is, this code can match different mutations with different types and different variables:

      const importExportMutationState = useMutationState({
        filters: {
          mutationKey: ["importdb"],
          exact: true,
          predicate: (mutation) =>
            mutation.state.variables?.fname === "arbitrary filename here",
        },
      })[0];

    exact: true can't do any type narrowing, and even if it could, mutationKey: ["importdb"] doesn't tell us that there is a mutation with those variables. So what you want is currently not possible automatically - only by adding manual type annotations to useMutationState.

    as for placeholderData - this works fine for me, but you have to make the QueryKey a tuple with as const: https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ5wC+cAZlBCHAOQACMAhgHaoMDGA1gPRTr2swBaAI458VAFDjWEZvBLAANjFyoAXHABK6aVAAmAHhZRgjAOYAaOEZOmA2gF0AfHAC8iQlJks4AdxO6SeigQAEldVzgAIgBGSMk0LFE8AAoEcTgMuBFcPABpdDx1WypWenB6YFNmKksqBWAWGtJFZShUezh6VDhpWXN0zOz8ADFGdWSASldnAAVyEAb0ADoeVAgFADd0ZIBWCf7MuDAFPnQAC3XdXAARegZxsB4N4Ag0W4ZLIbwpl2c0w8OvW8tnM5i++TwwxaKg6bi+AH4luCCnB4fC4A4BgC4JxONiMgA9eGSfFwYAkODJLGkrJJCFQpQqVwuNzIRhXeSMdDhAA+POppJgeDA6AgFORkOhbWZbio1jMVDgfIF+IlDNaqCWfnZgWCYRlKHZ6E53JVGSm-xpGR4MGQUEYho5Jm5AG4zUQSfibXaHWqpZrtQEgqFdEsTKwFMgrqhkoHdSGJu7DujHuhnq9UO96EnMuo2U6ubo3YcPIQJkA

  4. TkDodo closed this

  5. I see it working in your example, nice :)

    Updated the example for placeholderData to what matches my issue and get the same result. Can not get it to work unfortunately.

    We a re using queryOptions approach you shared at some point back in time. Guessing this is where it breaks somehow.

    https://www.typescriptlang.org/play/?ssl=12&ssc=1&pln=13&pc=11#code/JYWwDg9gTgLgBAbzgVwM4FMCKz1QJ4A0cAjjvgPJgzAQB2qcAvnAGZQQhwDkAAjAIb0BAYwDWAeijp+wmAFpSuPFwBQK4XVTwA7sFoATFvyggAkvrgBeOACIAjDbUahcYf3D9gAc3rYllak0rRBU4OAAbYC0ALjgAChZgcJhcVFiAJXQNKH0AHi0oPS8iAqKAbQBdAD5ghEYASisq0LCSMjwAmno4hBbWtqUAaXQ8WLKuNw9vei4iLkitWdYklKhUCrh+BmctAj7WxXwAMVpYuMbLGoAFdhAo9AA6KVQIcIA3dDiAVnq9-oa9owANxqNBYdo9fYPaGTMCeHyoPwUKhdVAPBYwHoA-b7MDhGToAAWr30uAAIvwBGcwFI3jQ0BSBERDngLjVev1+jt4GUCAQWcM8EcVqkNtYWQB+B4CkZwCUSuCVfb9cTiTmtAB6ErU6rCwBY8WVuplQpFayslmsyAM6EStHQFgAPo6jeqYHgwOgIAaTcLkqkLdYuKVaF4uHBna7Ob6zWjdAYjCZzIGUDa7Q6o40Obr+lIYMgoLRU6T0-oQTnGDrdXmC0WY-61g944ZjGZ9A89MJwshSag4s3E236lH+gqaeg6RAGZT+CPWrFrSW9A7y61Kw0gA

    Any more insides? 🤔


    For the useMutationState here's an example of how it used to work

    https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgVwM4FMCyyYEMbAQB2ANChtngcQMpXpwC+cAZlBCHAOQACeRqPAGMA1gHoo6XEJgBaAI7J0UAJ5cAUOqHFBcEDnyEiAaXQrUcALyJ1cO3AwwaAC2AsYALjgBtLo5duMFwAunC4FtoCMCS29pIgEABu6AHuXr7xSSmu7iFhETrR6owA3JpoWAbURAAUCLF2+lRGpipeTYbEragAdP45RfZ6VUYAYkReNQCUVgB8cAAK7CDAGD2SqBAANsk1AKxTMYxTmlqFcKsAMhC4ACbARADmVuSVzbT0dQ2swFswyqgvPUhkMwJJ7kJ8Oh2iNiHNviD7DUOtUehAwNVeiiWmYrJZrNiumZev1AnAAD7khGIkGEohojFGLGwky4-EElnddboBLJVIwGYAMkF1MRdL69B6iVwUGAuAARlt0KgAPwS3AsFiYHny5Q9YC3PHWQQarU6vUGuDC0W0lnq-5SmVyxXKtW3XAqACStwAHka4O6vb6YkNGCH7BglTIYe8iHNhrH7eh7Wh-VwwOgiPcnlwjlM+hx0DUrjds89LPMS3cHo8piUgA

  6. For the useMutationState here's an example of how it used to work

    with which version of react-query did this work?

    We a re using queryOptions approach you shared at some point back in time. Guessing this is where it breaks somehow.

    Thanks for showing me this. I think I broke that with:

    This is also related to:

    I need to find a better way to fix it 😅

  7. The version I updated from was 5.60.5 - now using 5.62.3.

    It breaks on 5.61.0 The version just before still works.

  8. shouldnt this issue be re-opened then?

  9. @bastibuck here’s the playground with types from v5.60.5. As you can see, it also doesn’t work.

    To the best of my knowledge, the mutation in the predicate filter was always of type Mutation<unknown, Error, unknown, unknown>.

  10. The mutation passed to useMutationState is of type Mutation<unknown, Error, unknown, unknown>, and I don't think it has ever been anything. The issue title seems to imply that it did work sometime in the past? Can you create a reproduction with a version where this worked?

    https://codesandbox.io/p/devbox/relaxed-scooby-9rgqft?workspaceId=ws_VPZaJaqxqdr5erejxx17SR

    only change: react query version to an older one that I said in original issue

  11. @bastibuck here’s the playground with types from v5.60.5. As you can see, it also doesn’t work.

    To the best of my knowledge, the mutation in the predicate filter was always of type Mutation<unknown, Error, unknown, unknown>.

    Are you sure this is actually using the types defined in the comment in the playground?

    Locally when installing 5.60.5

    • the mutation parameter is of type (parameter) mutation: Mutation<any, any, any, unknown>
    • where the predicate is defined as (property) MutationFilters.predicate?: ((mutation: Mutation<any, any, any>) => boolean) | undefined

    The corresponding type has been changed here https://github.com/TanStack/query/commit/175757afe633c7aa3eb665b95a81944c8c08c754#diff-9701bc1ef4ed4515a08de71beb3a01aac6de69115ceda88ad67995f1d5feab8cR62

  12. right, okay, thanks for digging out the commit. It doesn’t mean it “worked” before. The type was just any, which is really unsafe, and now it’s “unknown”, which forces you to think about / narrow it a bit. This is a change for the better.

  13. what is the proper way to avoid the typescript error then? casting?

    (mutation) => (mutation.state.variables as {fname: string})?.fname === fileentry.name

    maybe the docs should be updated here then: https://tanstack.com/query/latest/docs/framework/react/guides/filters#mutation-filters

Sign in to join this conversation.