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

setQueryData: NoInfer loses discriminated-union members when spreading a narrowed updater value

Abierto
#11,795 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
typescript

Línea de trabajo

Start with the minimal TypeScript reproduction and the setQueryData updater type definitions; inspect how the queryOptions key determines the updater parameter and how narrowing a discriminated union is represented. Run the provided tsc command, and consider the issue complete when the unannotated updater compiles while preserving the expected variant-specific fields without regressing related type behavior.

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

Descripción

Describe the bug

With @tanstack/[email protected], setQueryData infers the correct data type from a queryOptions key, but narrowing and spreading that union inside the updater produces a false assignability error. Explicitly annotating the updater parameter as Data | undefined makes the same code compile.

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: ['probe'],
  queryFn: async (): Promise<Data> => ({ kind: 'a', value: 1, requiredA: 1 }),
});
const client = new QueryClient();

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

Run with TypeScript 5.8.3:

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

TS2345: the callback return is inferred as NoInfer<Data> | { value: number; kind: 'a' | 'b' } | undefined. The spread loses the variant-specific required fields, so the returned object is rejected as Data.

Expected behavior

After the guard, spreading prev should preserve kind: 'a' and requiredA. The updater should compile without repeating the data type already encoded in the query key.

Workaround

Changing only (prev) to (prev: Data | undefined) compiles.

Versions and related context
  • @tanstack/react-query: 5.104.0
  • TypeScript: 5.8.3
  • Reproduces during typechecking; no browser or network calls needed.

Related: #11018 and #11245 address the useQuery return type. This report concerns the remaining NoInfer interaction in QueryClient.setQueryData updater parameters. The inferred key type itself is correct; the failure occurs during union narrowing/spread.

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

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.