Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
2 replies
onestyle

Share types between front and and backend without trpc...

I'm working on a project using nextjs, Hono js, drizzle, turborepo . I don't want to use trpc and would like to enforce types. I want to infer the return type of the fetchMyCollection to export it and use it in the nextjs app.

I tried to do type myType = ReturnType<typeof fetchAlbum> but this is only a shallow type. How can properly share the types across the backend and nextjs?



export async function fetchMyCollection(collectionItemId: string) {
  const collectionItem = await myDatabase.query.collectionItems.findFirst({
    where: eq(collectionItems.itemId, collectionItemId),
  });
  console.log("Fetched collection item: ", collectionItemId);

  const resourcesCount = (
    await myDatabase.select({
      count: sql<number>`count(*)`,
    }).from(myMediaResources).where(eq(myMediaResources.collectionItemId, collectionItemId))
  )[0].count;
  return { collectionItem: { ...collectionItem, resourcesCount }, resources: [] };
}
Was this page helpful?