query
query copied to clipboard
Svelte: passing a store to createQuery breaks types when using `select`
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
- create a query in svelte,
- pass a
derivedstore, - use
selectin the query, - notice how
datatype isany.
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
Platform
all
Tanstack Query adapter
svelte-query
TanStack Query version
^5.49.1
TypeScript version
6.5.2
Additional context
No response
might be related to my bug report: #7668
This may be fixed by #6981