grammyjs / grammyjs/website

Docs sample code leads to multiple type errors on `type: "multi"` sessions.

Open
#1,210 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
52
Forks
138
PR merge metrics
No merged PRs in 30d

Description

Copy pasting [the docs](https://grammy.dev/plugins/session#multi-sessions) leads to the following code:

```typescript
import { Bot, Context, session, SessionFlavor } from "https://deno.land/x/grammy@v1.35.0/mod.ts";
import { freeStorage } from "https://deno.land/x/grammy_storages@v2.5.1/free/src/mod.ts";

// Mock token for testing
const BOT_TOKEN = "example-token";

// Define the session interface matching the docs example
interface SessionData {
foo?: string;
bar: { prop: number };
baz: { width?: number; height?: number };
}

// Create context type
type MyContext = Context & SessionFlavor;

// Create bot instance
const bot = new Bot(BOT_TOKEN);

// Set up multi-session exactly as in the docs
bot.use(
session({
type: "multi",
foo: {
// these are also the default values
initial: () => undefined,
getSessionKey: (ctx) => ctx.chat?.id.toString(),
prefix: "",
},
bar: {
initial: () => ({ prop: 0 }),
storage: freeStorage(bot.token),
},
baz: {},
}),
);

// Simple echo middleware
bot.on("message", async (ctx) => {
console.log("Session data:", ctx.session);
await ctx.reply("Hello!");
});

// Run the bot
bot.start();
```

Which gives you:
```
t bot = new Bot(token: BOT_TOKEN);
session
Type '{ readAllKeys(): Promise<{ prop: number; } | undefined>; read(key: string): Promise<{ prop: number; } | undefined>; write(key: string, data: { prop: number; } | undefined): Promise<...>; delete(key: string): Promise<...>; getToken(): Promise<...>; }' is not assignable to type 'StorageAdapter<{ prop: number; }>'.
The types returned by 'readAllKeys()' are incompatible between these types.
Type 'Promise<{ prop: number; } | undefined>' is not assignable to type 'Iterable | AsyncIterable'.deno-ts(2322)
session.ts(155, 5): The expected type comes from property 'storage' which is declared here on type 'SessionOptions<{ prop: number; }, Context & SessionFlavor>'
```

If I somehow fix the storage issue I also get
```
Type '"multi"' is not assignable to type '"single"'.deno-ts(2322)
```

But I'm not sure how to repro that error anymore as I'm trying a bunch of stuff.

Contributor guide

Open the contributing guide

Research direction

Start with the session multi-sessions documentation at the linked page and reproduce the sample using the TypeScript and package versions shown. Compare the example with the current session and storage type definitions, including the reported `type: "multi"` error. Done when the copied sample type-checks without these errors, or the documentation clearly reflects the supported API.

Written by the indexing model from the issue text.

Assessment

Tech stack
deno, typescript
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.