HonoH
Hono13mo ago
pwuexec

Generic Types Not Working as Expected

I'm facing an issue with generic types in a PaginatedResponse setup. Here's my use case:

Code Example:


export interface PaginatedResponse<T> {
  items: T[];
  metadata: {
    total: number;
    page: number;
    limit: number;
    pages: number;
  };
}

export type BookingWithRelations = Prisma.BookingGetPayload<{
  select: {
    startTime: true;
    endTime: true;
    title: true;
    status: true;
    host: {
      select: {
        id: true;
        name: true;
        image: true;
      };
    };
    participants: {
      select: {
        id: true;
        name: true;
        image: true;
      };
    };
  };
}>;

// Returning this structure in a JSON response
return c.json<PaginatedResponse<BookingWithRelations>>(
  {
    items: [],
    metadata: {
      limit: 1,
      page: 1,
      total: 1,
      pages: 1,
    },
  },
  200
);


The Problem:

The client-side response lacks type safety. Even though the generic type PaginatedResponse<BookingWithRelations> is correctly defined, the returned response doesn't enforce or reflect the expected type safety.

I'm unsure if this is due to a limitation in the type inference, serialization, or something else entirely.

---

Question:

How can I ensure type safety is preserved in the client for such scenarios?
Any ideas or workarounds would be greatly appreciated! 🙏

---
Was this page helpful?