TanStack / TanStack/query

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

Open
#7,673 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

package: svelte-query
Dominant language
TypeScript
Stars
50.3k
Forks
4.2k
Avg merge
18h 25m
Merged PRs (30d)
200

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in the linked TypeScript playground, comparing createQuery with a direct options object and with a derived store. Start at the Svelte createQuery typing entry point and trace how select receives its data type. Done means select is inferred correctly when options are passed through a derived store, without breaking the existing direct-object behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.