Skip to content

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

@adriangalilea

Description

@adriangalilea

Copy pasting the docs leads to the following code:

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<SessionData>;

// Create bot instance
const bot = new Bot<MyContext>(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<MyContext>(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<string> | AsyncIterable<string>'.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<SessionData>>'

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions