setQueryData: NoInfer loses discriminated-union members when spreading a narrowed updater value
维护者通常 1 天内回复
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 55/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- typescript
调研方向
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.
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- TypeScript
- 星标
- 50.4k
- 派生
- 4.2k
- 平均合并
- 11 小时 33 分钟
- 30 天内合并 PR
- 421
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/query 的其他 Issue
-
solid-query: STRICT_READ_UNTRACKED on Solid 2 — client()/options() read in component body (useMutation, useBaseQuery)可能重新可做 关联的 PR 已关闭且未合并。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11358 · 2 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
solid-query: switching the queryClient accessor strands the new client's cache (subscription stays on the old observer)可能已有人在做 @MaNaN1803 于 74 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
TanStack/query#11106 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 4/5 3-5 天 新手友好度 45/100
维护者通常 1 天内回复
-
setQueryData updater loses discriminated-union fields when spreading inferred NoInfer data可能已有人在做 @boriskozak 今天认领。 未关闭
难度 4/5 3-5 天 新手友好度 68/100
TanStack/query#11794 · 1 条评论 ·
维护者通常 1 天内回复
-
[vue-query]: UseMutationReturnType default names unexported MutationResult (TS2883) 🤖🤖🤖可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 68/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复