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

Svelte: passing a store to createQuery breaks types when using `select`

未关闭
#7,673 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
frontend

调研方向

在链接的 TypeScript playground 中重现该问题,将 createQuery 分别与直接的 options 对象和派生 store 进行比较。从 Svelte 的 createQuery 类型定义入口开始,跟踪 select 如何接收其数据类型。当通过派生 store 传递 options 时 select 能够被正确推断,且不会破坏现有的直接对象行为时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

package: svelte-query
Describe the bug

using the latest version of tanstack query svelte, passing derived store and using select to select subset of the data makes the data in select type any and breaks the query types.

Playground link: https://tsplay.dev/mpkn6N

Example. This works:

// query type works as expected
const queryThatWorks = createQuery(
  derived(intervalMs, ($intervalMs) => ({
    queryKey: ["refetch"],
    queryFn: async () => ({ a: { b: { c: "" } } }),
    refetchInterval: $intervalMs,
  })),
);
const result1 = get(queryThatWorks)?.data;
//     ^? const result: {a: Eb: { c: string; 3; };..

this doesn't:

// `select` data is type any
const queryWithBrokenTypes = createQuery(
  derived(intervalMs, ($intervalMs) => ({
    queryKey: ["refetch"],
    queryFn: async () => ({ a: { b: { c: "" } } }),
    refetchInterval: $intervalMs,
    // data type is any, never inferred
    select: (data) => data,
    //       ^? (parameter) data: any
  })),
);
const result2 = get(queryWithBrokenTypes)?.data;
//     ^? const result2: any

this shows that select works without derived:

// `select` works without `derived`
const queryWithSelectWorks = createQuery({
  queryKey: ["refetch"],
  queryFn: async () => ({ a: { b: { c: "" } } }),
  // data type is any, never inferred
  select: (data) => data,
  //       ^? (parameter) data: {a: {b: { c: string; ) ;..
});

const result3 = get(queryWithSelectWorks)?.data;
//     ^? const result3: { a: {b: (c: string; ); );
Your minimal, reproducible example

https://tsplay.dev/mpkn6N

Steps to reproduce
  1. create a query in svelte,
  2. pass a derived store,
  3. use select in the query,
  4. notice how data type is any.

see twoslash shiki comments

Expected behavior

it should not break types when passing a store and using select to createQuery

How often does this bug happen?

Every time

Screenshots or Videos

CleanShot 2024-07-04 at 19 06 19

Platform

all

Tanstack Query adapter

svelte-query

TanStack Query version

^5.49.1

TypeScript version

6.5.2

Additional context

No response

主要语言
TypeScript
星标
50.4k
派生
4.2k
平均合并
13 小时 33 分钟
30 天内合并 PR
383

环境准备

从这里开始

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

TanStack/query 的其他 Issue

查看 TanStack/query 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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