icetouch
icetouch
HHono
Created by Paul Dlug on 8/10/2024 in #help
Type safety for middleware that sets context variables
Sorry for hijacking this thread, but I'm facing a similar issue and the above solution doesn't seem to work for me.
const app = new Hono();

type Env = {
Variables: {
var: boolean;
};
};

const echoMiddleware = createMiddleware<Env>(async (c, next) => {
await next();
});

app.get("/echo", echoMiddleware, (c) => {});
const app = new Hono();

type Env = {
Variables: {
var: boolean;
};
};

const echoMiddleware = createMiddleware<Env>(async (c, next) => {
await next();
});

app.get("/echo", echoMiddleware, (c) => {});
This returns error Type 'BlankEnv' is not assignable to type 'Env'. when using echoMiddleware in my get handler. It seems like app defaults to BlankEnv and does not work with middlewares with custom variables. Defining Variables when initializing Hono() fixes the issue but just like OP, I do not want all other handlers to get access to it.
11 replies