Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#11,795 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@iosayin 已经在做这个了。

开始于 2026年10月4日。

  • #11914 来自 @iosayin —— 未关闭
  • #11915 来自 @boriskozak —— 未关闭

评估

难度
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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/query 的其他 Issue

查看 TanStack/query 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。