openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Improperly typed mutation hook onError & onSettled

Open sacha-c opened this issue 2 months ago • 2 comments

openapi-react-query version

0.5.1

Description

The useMutation hook appears to have incorrectly typed options, which prevent the result of the onMutate callback to be used as parameters to the onError and onSettled callbacks.

I believe the cause to be the type definitions here: https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-react-query/src/index.ts#L159, where UseMutationOptions is set without generic parameter for TOnMutateResult which then gets set to unknown instead of the proper type.

I am not well-versed enough in types to fully understand the underlying issue so the problem could be elsewhere, or it may be an issue of the version of react-query which I have (@tanstack/[email protected]), but I think this projects supports this version so I believe this to be more of a bug.

I also noticed this issue https://github.com/openapi-ts/openapi-typescript/issues/2485, which potentially could provide a workaround for this issue (if it properly types those mutation options there), however it should still be possible to use mutation options the way I am trying to do so here.

Reproduction

A minimal reproduction can be found here: https://github.com/sacha-c/openapi-typescript/commit/cff269d1e5005ac6b9222208e8ae2b434693ede2

which simply adds the following, in which we can see that the mutation options are unknown instead of the result of the onMutate callback. I would expect the type to be { someArray: Array<number>, someString: string }

Image

Expected result

I expect the types of the onSettled & onError functions of the useMutation hook to be typed with the result from the onMutate function.

Extra

sacha-c avatar Nov 04 '25 18:11 sacha-c

After further digging, I think I found a solution. Posted the following PR for review: https://github.com/openapi-ts/openapi-typescript/pull/2519

sacha-c avatar Nov 04 '25 20:11 sacha-c

please merge šŸ™šŸ¾

JClackett avatar Nov 10 '25 21:11 JClackett