H
Hono4w ago
edproton

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
);
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! 🙏
---
2 Replies
ambergristle
ambergristle4w ago
there are a few patterns that can break hono client type inference, namely: - abstracting controller logic without using createFactory - defining method handlers as independent statements (rather than chaining) for more info, check out the best practices docs: https://hono.dev/docs/guides/best-practices
Best Practices - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
ambergristle
ambergristle4w ago
it's hard to say more without knowing how you've set up your backend

Did you find this page helpful?