withPagination type inference

Looking for help as to why my types on this withPagination function are not getting inferred correctly. Data does on the
PaginatedResult
does not get full inferred at the top level.

pagination.ts

import type { PgSelect } from 'drizzle-orm/pg-core'
import { db } from './db'
import { count } from 'drizzle-orm'

export interface PaginatedOptions {
    page?: number
    perPage?: number
}

export interface PaginatedResult<T> {
    data: T[]
    page: number
    perPage: number
    pageCount: number
    totalCount: number
}

export async function withPagination<T extends PgSelect>(
    qb: T,
    pagination?: PaginatedOptions
): Promise<PaginatedResult<T>> {
    const page = pagination?.page ?? 1
    const perPage = pagination?.perPage ?? 10
    const query = qb.limit(perPage).offset((page - 1) * perPage)
    const sq = db.$with('sq').as(query)

    const data = (await query.execute()) as T[]
    const meta = await db.select({ count: count() }).from(sq)

    const totalCount = Number(meta[0].count)
    const pageCount = Math.ceil(totalCount / perPage)

    return {
        data,
        totalCount,
        pageCount,
        perPage,
        page,
    }
}


user.service.ts

export async function list(filters?: any, pagination?: PaginatedOptions) {
     const query = db.select().from(userTable).$dynamic()
     return await withPagination(query, pagination)
 }


api.ts

import { Hono } from 'hono'
import { handle } from 'hono/aws-lambda'
import { user } from '@rally/core'

const app = new Hono()

app.get('/', async (c) => {
    // data here is type any
    const { data: users } = await user.list()
})

export const handler = handle(app)
Was this page helpful?