Skip to content

[lit-query] infiniteQueryOptions does not have DataTag on queryKey #11142

Description

@tin-serdar

Describe the bug

In the lit-query package, infiniteQueryOptions does not have the DataTag enrichment for the queryKey.

This leads to missing queryFn types when using setQueryData.

In the example, when hovering over querySlugType you can see the queryFn types injected with [dataTagSymbol]. Those are missing on infiniteQuerySlugType.

Checking the infiniteQueryOptions.ts, it is easy to see that the queryKey: DataTag<TQueryKey, InfiniteData<TQueryFnData>, TError> part is missing.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-mh2mvfum?file=src%2Fquery.ts

Steps to reproduce

  1. Define an example exampleInfiniteQueryOptions with infiniteQueryOptions that has a queryKey and queryFn.
export const exampleInfiniteQueryOptions = (slug: string) => {
  return infiniteQueryOptions({
    queryKey: ['website', slug],
    queryFn: async ({pageParam}) => {
      const response = await fetch(`https://example.com/${slug}?cursor=${pageParam}`);
      if (!response.ok) {
        throw new Error(response.statusText); 
      }
      return response.json() as Promise<{previous: number, next: number, data: string}>;
    },
    initialPageParam: 0,
    getPreviousPageParam: (firstPage) => firstPage.previous,
    getNextPageParam: (lastPage) => lastPage.next,
  })
}
  1. Use the defined exampleInfiniteQueryOptions().queryKey in setQueryData
queryClient.setQueryData(infiniteQuerySlugType, (old) => {return {previous: 1, next: 3, data: [...old.data, 'queryOptions2']};});
  1. typescript complains about the old.data unknown type

Expected behavior

  1. Define an example exampleInfiniteQueryOptions with infiniteQueryOptions that has a queryKey and queryFn.
export const exampleInfiniteQueryOptions = (slug: string) => {
  return infiniteQueryOptions({
    queryKey: ['website', slug],
    queryFn: async ({pageParam}) => {
      const response = await fetch(`https://example.com/${slug}?cursor=${pageParam}`);
      if (!response.ok) {
        throw new Error(response.statusText); 
      }
      return response.json() as Promise<{previous: number, next: number, data: string}>;
    },
    initialPageParam: 0,
    getPreviousPageParam: (firstPage) => firstPage.previous,
    getNextPageParam: (lastPage) => lastPage.next,
  })
}
  1. Use the defined exampleInfiniteQueryOptions().queryKey in setQueryData
queryClient.setQueryData(infiniteQuerySlugType, (old) => {return {previous: 1, next: 3, data: [...old.data, 'queryOptions2']};});
  1. typescript correctly infers about the old.data type

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Platform and browser agnostic, typescript issue

Tanstack Query adapter

None

TanStack Query version

0.2.11

TypeScript version

6.0.2

Additional context

lit-query does not yet exist as the option for Tanstack Query adapter field in bug report

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions