H
Hono3w ago
lambert

RPC typing is behaving weird

I'm not sure if this might be an issue with my setup or with Hono itself, but I'm a bit at a loss here I have a Hono router for projects which I want to use as a projectsClient with hc. For this I take my projects Router and extract the Type like this:
type ProjectsRouter = typeof projectRoutes;
// The resulting type is:
type ProjectsRouter = Hono<BlankEnv, {
"/": {
$get: {
input: {};
output: {
projects: {
name: string;
id: string;
image: string | null;
url: string | null;
slug: string;
description: string | null;
}[];
};
outputFormat: "json";
status: 200;
};
};
} & {
...;
}, "/">
type ProjectsRouter = typeof projectRoutes;
// The resulting type is:
type ProjectsRouter = Hono<BlankEnv, {
"/": {
$get: {
input: {};
output: {
projects: {
name: string;
id: string;
image: string | null;
url: string | null;
slug: string;
description: string | null;
}[];
};
outputFormat: "json";
status: 200;
};
};
} & {
...;
}, "/">
Now I import ProjectsRouter in my frontend and my "/" output changes to [x: string]: any;
type ProjectsRouter = Hono<BlankEnv, {
"/": {
$get: {
input: {};
output: {
[x: string]: any;
};
outputFormat: "json";
status: 200;
};
};
} & {
"/": {
$post: {
input: {
json: {
name: string;
slug: string;
image?: string | undefined;
url?: string | undefined;
description?: string | undefined;
};
};
output: {
...;
};
outputFormat: "json";
status: ContentfulStatusCode;
};
};
}, "/">
type ProjectsRouter = Hono<BlankEnv, {
"/": {
$get: {
input: {};
output: {
[x: string]: any;
};
outputFormat: "json";
status: 200;
};
};
} & {
"/": {
$post: {
input: {
json: {
name: string;
slug: string;
image?: string | undefined;
url?: string | undefined;
description?: string | undefined;
};
};
output: {
...;
};
outputFormat: "json";
status: ContentfulStatusCode;
};
};
}, "/">
I thought it might by my Typescript version or any other version but they work fine. Any other type exported from my backend to frontend works too. Anyone might have an idea here? Thanks in advance!
19 Replies
ambergristle
ambergristle3w ago
hey! idk if this is what's happening here, but some transpilers are known to elide types when you're exporting them across project boundaries that means that they'll simplify nested types (or even swallow them entirely) how are you managing the monorepo, and how are you sharing types across the boundary? for best performance, you'll want to be exporting generated types
lambert
lambertOP3w ago
Hey, I‘m using Bun and Turborepo for managing my monorepo and I‘m importing them directly from the source file How would I go about exporting a generated type and still keep the DX somewhat good?
ambergristle
ambergristle3w ago
i've never used turborepo, but here is an example shared previously: https://github.com/m-shaka/hono-rpc-perf-tips-example/blob/main/apps/server/tsconfig.build.json
lambert
lambertOP3w ago
Found that too, thank you! Now I have problems with better-auths type being too big, but that's another story! Thank you very much for your help!
ambergristle
ambergristle3w ago
np! tbh, all this bundling/build stuff is still a bit over my head
lambert
lambertOP3w ago
Quite honestly, for me too.. It has grown so large it's crazy
ambergristle
ambergristle3w ago
my sense is that better-auth types probably can be excluded from the generated defs but idk how you'd go about that i will say i tend to avoid tools like turborepo, or anything that promises to do it all/manage it for me
lambert
lambertOP3w ago
Okay, wait, now I'm even more confused... This is the type that's getting outputted:
api: {
projects: import("hono/client").ClientRequest<{
$get: {
input: {};
output: {};
outputFormat: "json";
status: 200;
};
$post: {
input: {};
output: {};
outputFormat: "json";
status: import("hono/utils/http-status").ContentfulStatusCode;
};
}>;
};
api: {
projects: import("hono/client").ClientRequest<{
$get: {
input: {};
output: {};
outputFormat: "json";
status: 200;
};
$post: {
input: {};
output: {};
outputFormat: "json";
status: import("hono/utils/http-status").ContentfulStatusCode;
};
}>;
};
ambergristle
ambergristle3w ago
regardless of context, home-rolled tends to be faster, cheaper, and easier
lambert
lambertOP3w ago
Yeah, for real projects I'd do that too but this is a vacation fun project, just want to do something quick and dirty
ambergristle
ambergristle3w ago
sounds like you've definitely got dirty. idk about quick though, lol but i hear you. i've wasted the last week reinventing the rate-limiting wheel instead of getting a side-project deployed
lambert
lambertOP3w ago
Curse of programming, should've just booked a vacation instead of doing even more programming lol
ambergristle
ambergristle3w ago
porque no los dos? you could be banging your head against the sand on a beautiful beach
lambert
lambertOP3w ago
Was an ad-hoc vacation, not really time to plan anything 😄
ambergristle
ambergristle3w ago
oh word what are you working on?
lambert
lambertOP3w ago
Sorry, had to do something A translation management system
ambergristle
ambergristle3w ago
sick. like for localizing a web page/app? or translating text and/or speech?
lambert
lambertOP3w ago
Localizing software, yeah, like i18n and other stuff
ambergristle
ambergristle3w ago
dope i feel like it's always an afterthought, which it shouldnt be but i get that it's also a complicated problem

Did you find this page helpful?