Skip to content

useInfiniteFindMany does not infer lastPageParam type #2426

@motopods

Description

@motopods

Description and expected behavior

When using useInfiniteFindMany in ZenStack, the lastPageParam (and allPageParams) parameter inside getNextPageParam is inferred as unknown.

In real-world infinite query scenarios, we typically reuse lastPageParam to construct the next query arguments (instead of rebuilding the entire args object manually). However, since lastPageParam is typed as unknown, this pattern becomes unsafe and requires as any casts.

Under the hood, TanStack Query’s useInfiniteQuery correctly infers TPageParam from initialPageParam. It appears that ZenStack’s wrapper does not properly forward the generic parameter, causing TPageParam to default to unknown.

Environment (please complete the following information):

  • ZenStack version: 3.4.1

Additional context

const PAGE_SIZE = 20

const query = zen.user.useInfiniteFindMany(
  {
    select: {
      id: true,
      name: true,
      email: true,
      createdAt: true,
    },
    where: {
      isActive: true,
      createdAt: {
        gte: new Date('2024-01-01'),
      },
    },
    orderBy: { createdAt: 'desc' },
    take: PAGE_SIZE,
  },
  {
    getNextPageParam: (lastPage, _allPages, lastPageParam: unknown) => {
      if (lastPage.length < PAGE_SIZE) return undefined

      const lastUser = lastPage[lastPage.length - 1]

      // Common infinite-query pattern:
      // Reuse previous args and only modify pagination condition
      return {
        ...lastPageParam,
        where: {
          ...lastPageParam.where,
          id: {
            lt: lastUser.id,
          },
        },
      }
    },
  },
)

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