[fix] Context TS typing is not working
- Dominant language
- JavaScript
- Stars
- 414
- Forks
- 64
- PR merge metrics
- No merged PRs in 30d
Description
## 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
```ts
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
```ts
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**
```ts
import koa from "koa";
import session, { Session } from "koa-generic-session";
let store: Record = {};
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**
```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**
```json
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"esModuleInterop": true
}
}
```
## Checklist
- [x] I have searched through GitHub issues for similar issues.
- [x] I have completely read through the README and documentation.
- [x] 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:
```ts
const app = new koa();
```
Is this the intended use?
Contributor guide
Research direction
Start with the main.ts reproduction and tsconfig.json, then inspect the package's published TypeScript declarations and the Koa context typing used by the session middleware. Run the TypeScript compiler against the reproduction and compare the inferred type of ctx.session with the expected Session type. Done means session is typed as Session, foo is inferred as "bar", and assigning to session.cookie is rejected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100