Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Context TS typing is not working #142

Open
3 tasks done
QuentinRoy opened this issue Jan 10, 2023 · 3 comments
Open
3 tasks done

[fix] Context TS typing is not working #142

QuentinRoy opened this issue Jan 10, 2023 · 3 comments
Labels

Comments

@QuentinRoy
Copy link

QuentinRoy commented Jan 10, 2023

Describe the bug

Node.js version: 18.12.1

Typescript version: 4.9.4

Koa version: 2.14.1 (types: 2.13.5)

OS version: Mac OS 13.0.1

Description: Context's session is not properly typed despite the type definitions.

Actual behavior

declare module "koa-generic-session" {
  interface Session {
    foo: "bar";
  }
}

app.use((ctx) => {
  // session is any.
  let { session } = ctx;
  // This is fine.
  session.cookie = "hahahaha !";
  // foo is any
  let foo = session.foo;
});

Expected behavior

declare module "koa-generic-session" {
  interface Session {
    foo: "bar";
  }
}

app.use((ctx) => {
  // session is Session.
  let { session } = ctx;
  // The line below is a type error.
  // session.cookie = "hahahaha !";
  // foo is "bar"
  let foo = session.foo;
});

Code to reproduce

main.ts

import koa from "koa";
import session, { Session } from "koa-generic-session";

let store: Record<string, Session> = {};

declare module "koa-generic-session" {
  interface Session {
    foo: "bar";
  }
}

const app = new koa();
app.use(
  session({
    store: {
      get(key: string) {
        return store[key];
      },
      set(key: string, sess: Session) {
        store[key] = sess;
      },
      destroy(key: string) {
        delete store[key];
      },
    },
  }),
);

app.use((ctx) => {
  // session is any but should be Session.
  let { session } = ctx;
  // This is fine but shouldn't.
  session.cookie = "hahahaha !";
  // foo is any but should be "bar".
  let foo = session.foo;
});

app.listen(8080);

package.json

{
  "name": "ts-koa-session",
  "version": "1.0.0",
  "dependencies": {
    "@types/koa": "^2.13.5",
    "@types/koa-generic-session": "^2.2.1",
    "koa": "^2.14.1",
    "koa-generic-session": "^2.3.0",
    "typescript": "^4.9.4"
  }
}

tsconfig.json

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "esModuleInterop": true
  }
}

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Potential fix or workaround

At the moment, I need to manually define the context type for my app:

const app = new koa<DefaultState, Context>();

Is this the intended use?

@QuentinRoy QuentinRoy added the bug label Jan 10, 2023
@titanism
Copy link
Contributor

As maintainers we don't write nor support TS, but a PR is welcome!

@QuentinRoy
Copy link
Author

Thanks for the answer! This makes sense. Unfortunately, I am afraid I won't be able to work on this myself, at least in the time being. Just to clarify, you are talking about a PR to https://github.com/DefinitelyTyped/DefinitelyTyped, is it?

@titanism
Copy link
Contributor

titanism commented Feb 1, 2023

I think (?) not sure though!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants