setQueryData updater loses discriminated-union fields when spreading inferred NoInfer data
Maintainer antworten meist innerhalb von 1 Tag
@boriskozak arbeitet bereits daran.
Seit 04.10.2026.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 68/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- typescript
- Bereich
- api
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- TypeScript
- Sterne
- 50.4k
- Forks
- 4.2k
- Ø Merge
- 6 Std. 55 Min.
- Gemergte PRs (30 T.)
- 390
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus TanStack/query
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
TanStack/query#11930 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)Evtl. wieder frei @LeonxLJX hat das vor 35 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
TanStack/query#11358 · 2 Kommentare · 1 Reaktion ·
Maintainer antworten meist innerhalb von 1 Tag
-
solid-query: switching the queryClient accessor strands the new client's cache (subscription stays on the old observer)Evtl. vergeben @MaNaN1803 hat das vor 78 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
TanStack/query#11106 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
restoreQueries and persisterGc throw on malformed persisted entriesEvtl. vergeben @VGontier-cmd hat das vor 3 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 45/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
[Feature]: [P3] engine-rs: the package source hash should ignore line endings and untracked filesOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
maniator/verticopolis#880 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
siyuan-note/siyuan#20353 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
black-forest-labs/skills#17 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Albert-Weasker/niubigeo#168 ·
Maintainer antworten meist innerhalb von 1 Tag