Skip to content

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

@mukeshdhadhariya

Description

@mukeshdhadhariya

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[].

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions