H
Hono5d ago
amorfati

correct type of this `Context`

from here https://hono.dev/docs/middleware/builtin/timeout#usage I tried
context: Context<{ Bindings: Env }>
context: Context<{ Bindings: Env }>
but it's not right: "Property 'headers' does not exist on type 'HonoRequest<any, unknown>'"
No description
13 Replies
amorfati
amorfatiOP5d ago
app.use('*', timeout(4_000, (context) => {
context.req.headers // <- 'Property 'headers' does not exist on type 'HonoRequest<any, unknown>''
}))
app.use('*', timeout(4_000, (context) => {
context.req.headers // <- 'Property 'headers' does not exist on type 'HonoRequest<any, unknown>''
}))
ambergristle
ambergristle5d ago
export type MiddlewareHandler<
E extends Env = any,
P extends string = string,
I extends Input = {}
> = (c: Context<E, P, I>, next: Next) => Promise<Response | void>;

export declare class Context<
E extends Env = any,
P extends string = any,
I extends Input = {}
> {
env: E["Bindings"];
// ...
}
export type MiddlewareHandler<
E extends Env = any,
P extends string = string,
I extends Input = {}
> = (c: Context<E, P, I>, next: Next) => Promise<Response | void>;

export declare class Context<
E extends Env = any,
P extends string = any,
I extends Input = {}
> {
env: E["Bindings"];
// ...
}
if you're just using the vanilla Env from hono, this should be fine:
const customError = (c: Context) => {}
const customError = (c: Context) => {}
Arjix
Arjix5d ago
personally I just gave up and used a bunch of // @ts-ignores although that code has since been deleted, because I fully migrated away from express
ambergristle
ambergristle5d ago
@ts-ignore should be a tool of last resort Hono typing can be finicky, but if TS is telling you not to do something, there’s usually a good reason Some code debt is always part of rapid early-stage development, but compromising on typing is a recipe for mid/long-term pain and app instability I’ve worked for companies that lost customers because they cut TS corners early on and wound up with unreliable data
Arjix
Arjix5d ago
trust me, I am a ts wiz embedding hono as middleware in an existing expressjs app and trying to pass down the remoteAddress from express down to hono was a temporary solution, so I didn't care about the type
ambergristle
ambergristle5d ago
respectfully, i'm going to trust my own understanding of typescript, and my professional experience
Arjix
Arjix5d ago
TL;DR it was a last resort
ambergristle
ambergristle5d ago
you may have been comfortable hiding code from TS, but i am not, *and i would strongly recommend against using @ts-ignore unless there is truly no other option, and even then only in a limited scope
Arjix
Arjix5d ago
I won't be spending more than 30 minutes trying to make typescript happy when working on a legacy codebase that I was going to rewrite in less than a week it was a horrible experience for me as well
Arjix
Arjix5d ago
No description
No description
No description
Arjix
Arjix5d ago
I got actual PTSD from that codebase, hacking it to add hono and eventually purge express was so nice
Arjix
Arjix5d ago
No description
Arjix
Arjix5d ago
I am happy with my warcrimes since they are now deleted

Did you find this page helpful?