Skip to content
Source
Sign inSign up
TanStack/query

useMutationState loses type inference in select callback due to non-generic Mutation type #10463

Closedmukeshdhadhariya(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

useMutationState does not preserve type information inside the select callback. The mutation parameter is typed as a non-generic Mutation, causing mutation.state.data to be inferred as unknown.

Reproduction

const result = useMutationState({
  select: (mutation) => mutation.state.data
})

Your minimal, reproducible example

import type { Mutation } from '@tanstack/query-core'  type MutationStateOptions<TResult = unknown> = {   select?: (mutation: Mutation) => TResult }  function test() {   const options: MutationStateOptions = {     select: (mutation) => {       return mutation.state.data     },   }    const result = options.select?.({} as any)    // ❌ inferred as unknown   return result }

Steps to reproduce

  1. Use useMutationState with a select callback
  2. Access mutation.state.data inside select
  3. Observe the inferred type of the result

Expected behavior

The type of mutation.state.data should be correctly inferred based on the mutation's generic type (e.g., number, string, etc.), and the returned result should reflect that type (e.g., number[]).

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

For all

Tanstack Query adapter

None

TanStack Query version

@tanstack/react-query v5 (latest)

TypeScript version

No response

Additional context

The type of mutation.state.data is inferred as unknown because Mutation is used without generics. As a result, the final returned value from useMutationState is typed as unknown[].

  1. useMutationState can and will match multiple mutations, so there is no way we can infer the type to something here.

  2. TkDodo closed this

  3. TkDodo added labelsknown limitation

Sign in to join this conversation.