Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

setQueryData updater loses discriminated-union fields when spreading inferred NoInfer data

Abierto
#11,794 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@boriskozak ya está trabajando en esto.

Desde el 4/10/2026.

  • #11914 de @iosayin — abierto
  • #11915 de @boriskozak — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
api

Línea de trabajo

Start with the minimal reproduction and the QueryClient.setQueryData and queryOptions entry points, focusing on how the updater parameter uses NoInfer. Run the provided TypeScript command before and after changes. Done means the unannotated updater compiles, preserves the narrowed discriminated-union fields, and the reverse-inference guard remains intact.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Describe the bug

With @tanstack/react-query 5.104.0, an updater passed to setQueryData(queryOptions(...).queryKey, updater) fails when it narrows a discriminated union and spreads the narrowed value. The tagged key supplies the correct data type, but the inferred updater parameter is wrapped in NoInfer<Data>. The spread loses the variant-specific fields.

Explicitly annotating the updater parameter as Data | undefined makes the same update compile. This appears related to #11018, but affects the QueryClient cache updater rather than the useQuery result.

Minimal reproduction
import { QueryClient, queryOptions } from '@tanstack/react-query'

type Data =
  | { kind: 'a'; value: number; requiredA: number }
  | { kind: 'b'; value: number; requiredB: number }

const query = queryOptions({
  queryKey: ['example'],
  queryFn: async (): Promise<Data> => ({ kind: 'a', value: 1, requiredA: 1 }),
})
const client = new QueryClient()

// Correctly inferred from the tagged key.
const inferred: Data | undefined = client.getQueryData(query.queryKey)

// Fails with TS2345.
client.setQueryData(query.queryKey, (prev) => {
  if (!prev || prev.kind !== 'a') return prev
  return { ...prev, value: prev.value + 1 }
})

// The same update passes with an explicit parameter annotation.
client.setQueryData(query.queryKey, (prev: Data | undefined) => {
  if (!prev || prev.kind !== 'a') return prev
  return { ...prev, value: prev.value + 1 }
})
Steps to reproduce

Typecheck the example with TypeScript 5.8.3:

tsc --noEmit --strict --skipLibCheck --target es2023 --moduleResolution bundler --module esnext repro.ts

The unannotated updater fails with a return type containing { value: number; kind: "a" | "b" }, missing requiredA and requiredB. The annotated updater compiles.

Expected behavior

After prev.kind !== 'a' returns, spreading prev should preserve kind: 'a' and requiredA. The documented query-options key inference should allow this update without repeating the data type on the parameter.

Versions
  • @tanstack/react-query: 5.104.0
  • @tanstack/query-core: 5.104.0
  • TypeScript: 5.8.3
  • Platform: macOS; compile-time only
Additional context

The reproducer uses no application-specific wrappers, key registration, or casts. The reverse-inference guard should remain intact; this report is about preserving normal union narrowing and spread behavior while retaining that guard.

Lenguaje dominante
TypeScript
Estrellas
50.4k
Forks
4.2k
Merge medio
11 h 33 min
PR fusionados (30 d)
421

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de TanStack/query

Todos los issues de TanStack/query

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.