jcs224 / hono_sessions

Cookie-based sessions for Hono applications
https://jsr.io/@jcs224/hono-sessions
MIT License
68 stars 9 forks source link

how to fix the typescript error from app.use('*', sessionMiddleware(... #18

Open bkerin opened 2 months ago

bkerin commented 2 months ago

The home page said it might happen and shows how to ts-ignore, but I'd sort of like to know what it means and maybe how to fix it. For me without the ts-ignore deno says:

error: TS2769 [ERROR]: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'MiddlewareHandler' is not assignable to parameter of type 'MiddlewareHandler<{ Variables: { session: Session; session_key_rotation: boolean; }; }, "*", {}>'.
      Types of parameters 'c' and 'c' are incompatible.
        Type 'Context<{ Variables: { session: Session; session_key_rotation: boolean; }; }, "*", {}>' is not assignable to type 'Context<any, string, {}>'.
          Property '#private' in type 'Context' refers to a different member that cannot be accessed from within type 'Context'.
app.use('*', sessionMiddleware({
             ^
    at file:///home/bkerin/projects/test/web_server/deno_tsx_test.tsx:159:14

I'm pretty new to typescript and have to admit I find this pretty hard to decipher especially the mismatch between type 'Context' ... with type 'Context' bit. Any clues?

jcs224 commented 1 month ago

Don't worry, TypeScript is very confusing to me too :sweat_smile: I'm figuring it out as I go.

When you created your app, did you include the extra TypeScript generics?

const app = new Hono<{
  Variables: {
    session: Session,
    session_key_rotation: boolean
  }
}>()
DywiTom commented 3 days ago

The error appears if you use

import { Hono } from 'jsr:@hono/hono'

instead of

import { Hono } from 'npm:hono'