I am facing an issue with exporting a complex type created using the typeof
operator in Typescript. Here is the scenario:
// controller/createProfile.ts
import { z } from 'zod';
import { zValidator } from '@hono/zod-validator';
const createProfileBody = z.object({
id: z.string({
required_error: 'Id is required.',
}),
username: z.string({
required_error: 'User name is required.',
}),
firstname: z.string({
required_error: 'First name is required',
}),
lastname: z.optional(z.string()),
avatar: z.optional(z.string()),
});
const route = app.post('/', zValidator('json', createProfileBody), async c => {
...
});
export type CreateProfileType = typeof route;
// types.d.ts
export * from './controller/createProfile';
The exported type CreateProfileType
looks like this:
Hono<{
Bindings: Binding;
}, Schema<"post", "/", {
json: {
lastname?: string | undefined;
avatar?: string | undefined;
id: string;
username: string;
firstname: string;
};
}, ReturnType>>
When trying to import the CreateProfileType
into another module, the imported type always defaults to any
. I have specified the types
field in my package.json
and tried different configurations in the tsconfig
, but the issue persists.
Edit
After generating a declaration file using tsc
, I found that the type of route
is lost in the export process. Manually specifying the type of route
seems to resolve the issue, but it's not an ideal solution. Is there a way to export the correct type of route
for accessibility in different packages?