setQueryData updater loses discriminated-union fields when spreading inferred NoInfer data
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
- #11915 par @boriskozak — fermée sans fusion
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 68/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- typescript
- Domaine
- api
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- TypeScript
- Étoiles
- 50.5k
- Forks
- 4.2k
- Merge moyen
- 8 h 21 min
- PR mergées (30 j)
- 621
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de TanStack/query
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)Peut-être à nouveau libre @LeonxLJX l’a pris il y a 54 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
TanStack/query#11358 · 2 commentaires · 1 réaction ·
Les mainteneurs répondent en général sous 1 jour
-
useBaseQuery fetches the old query key when an <Activity> tree is revealedPeut-être pris @lazerg l’a pris il y a 7 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 35/100
Les mainteneurs répondent en général sous 1 jour
-
[query-broadcast-client-experimental] broadcastQueryClient: an `added` event overwrites a receiving query that has no data (e.g. in error) with the sender's empty statePeut-être pris @lazerg l’a pris il y a 8 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 25/100
Les mainteneurs répondent en général sous 1 jour
-
solid-query 6.0.0-rc.5: onCacheEvent throws NotReadyError during a Solid 2 (rc.14+) transition landingPeut-être pris @UzumakiODZ l’a pris il y a 13 jours. Ouverte
Difficulté 4/5 3-5 jours Accessibilité débutants 28/100
TanStack/query#12031 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Explicit gcTime schedules a GC timer during SSR, retaining the whole render in memoryPeut-être pris @AdzerKI l’a pris il y a 14 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 25/100
TanStack/query#12025 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de TanStack/query
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
vekexasia/pi-extensible-workflows#374 ·
Les mainteneurs répondent en général sous 2 jours
-
documentation good first issue
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
Les mainteneurs répondent en général sous 1 jour
-
help wanted linux
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
GiuseppeBellamacina/downtime#19 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
scaleway/ultraviolet#7089 ·
Les mainteneurs répondent en général sous 2 jours
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Get-Concord-AI/concord-mcp#296 ·
Les mainteneurs répondent en général sous 1 jour