TanStack / query

🤖 Powerful asynchronous state management, server-state utilities and data fetching for the web. TS/JS, React Query, Solid Query, Svelte Query and Vue Query.
https://tanstack.com/query
MIT License
42.4k stars 2.89k forks source link

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

Open o-az opened 4 months ago

o-az commented 4 months ago

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

PhilippMolitor commented 3 months ago

might be related to my bug report: #7668

lachlancollins commented 3 months ago

This may be fixed by #6981