-
Hi all, Really enjoying working with Hono but running into a typescript issue. I'm adding some helper functions to an NPM package and whenever I try passing the context I get typescript errors. Is there a proper way to accept a context in a 3rd party library? I'll start with the typescript errors: Argument of type 'Context<{ Variables: RequestVariables; }, "/public/*", {}>' is not assignable to parameter of type 'Context<any, any, {}>'.
Property '#private' in type 'Context' refers to a different member that cannot be accessed from within type 'Context'. function in package: import { Context } from "hono";
export function canHandleContext(c: Context) {
console.log(c.req.header("content-type"));
} The error happens over app.use("/public/*", (c, next) => {
canHandleContext(c);
c.res.headers.set("Cache-Control", "public, max-age=31536000");
return next();
}); Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
Created an example project to show the typescript error: https://github.com/travierm/enzo-ts-error-example |
Beta Was this translation helpful? Give feedback.
-
Probably because the Hono that |
Beta Was this translation helpful? Give feedback.
-
I'll give it a try @yusukebe thanks! |
Beta Was this translation helpful? Give feedback.
-
@yusukebe this is what I came up with. import { Context } from "hono";
import { CookieOptions } from "hono/utils/cookie";
import { StatusCode } from "hono/utils/http-status";
interface MinimalContext {
redirect: (location: string, status?: StatusCode) => Response;
cookie: (name: string, value: string, opt?: CookieOptions) => void;
notFound: () => Response | Promise<Response>;
header: (
name: string,
value: string | undefined,
options?: {
append?: boolean;
}
) => void;
}
export function canHandleContextTwo(c: MinimalContext) {
const context = c as Context;
console.log(context.req.header("content-type"));
} Its not perfect but it prevents users from passing something that is drastically different then the actual Hono context. |
Beta Was this translation helpful? Give feedback.
-
Hmm. I don't think it's a good solution. As I wrote before, it would be better to remove |
Beta Was this translation helpful? Give feedback.
I am sorry if I am wrong too.
What you are trying to do is the same as HonoX or 3rd party Middleware. So please refer to that.
https://github.com/honojs/middleware/tree/main/packages/zod-validator
That is, put
hono
in your package's devDependencies and do not includehono
in your package's dependencies. Users will installhono
with your package when they use it.This way, user applications can refer to the same
hono
and there will be no type errors.