TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
42.23k stars 2.87k forks source link

Angular injectMutation: `onSuccess` loses type when `queryClient` is passed to `injectMutation` #8042

Open 7schmiede opened 1 month ago

7schmiede commented 1 month ago

Describe the bug

When the queryClient is passed in the injectMutation method, the return type of mutationFn is lost in the onSuccess method.

Your minimal, reproducible example

https://stackblitz.com/edit/stackblitz-starters-3zsk2e?file=test.ts

Steps to reproduce

import { injectMutation } from "@tanstack/angular-query-experimental";

// without queryClient 
export const foo = injectMutation(() => ({
    mutationFn: () => Promise.resolve({ foo: true }),
    onSuccess: (result) => {
        result.foo; // result: { foo: boolean; }
    }
}));

// with queryClient
export const bar = injectMutation((queryClient) => ({
    mutationFn: () => Promise.resolve({ bar: true }),
    onSuccess: (result) => {
        result.bar; // result: unknown
    }
}));

Expected behavior

Even if the queryClient is passed, the return type of the mutationFn should remain in the onSuccess method.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Tanstack Query adapter

angular-query

TanStack Query version

v5.55.4

TypeScript version

No response

Additional context

No response

mevans commented 1 month ago

Yeah this is a really frustrating issue. I either have to choose between just injecting the query client at the top level, or manually typing all parameters in the callback functions

rklfss commented 3 days ago

Same here. Figured out, specifying the queryClient type does the trick for the moment. At least better than every method arguments.

injectMutation((client: QueryClient) => ({
}));
ThiloAschebrock commented 3 days ago

One could fix this by wrapping TData with NoInfer<TData> in the MutationOptions type of the core package at https://github.com/TanStack/query/blob/main/packages/query-core/src/types.ts#L1005, but that would be a breaking change for all adapters.