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

Add section in docs about TypeScript #1121

Merged
merged 4 commits into from
Sep 3, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix example code
  • Loading branch information
KnorpelSenf committed Sep 1, 2020
commit b84d3967404d2fefc5bef254c7d527a99fb09f9a
13 changes: 6 additions & 7 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,10 @@ Consequently, you can change the type of `ctx` to fit your needs in order for yo
This is done through Generics:

```ts
const { Telegraf } = require('telegraf')
const { TelegrafContext } = require('telegraf/typings/context')
import { Context, Telegraf } from "telegraf";

// Define your own context type
interface MyContext extends TelegrafContext {
interface MyContext extends Context {
myProp?: string
myOtherProp?: number
}
Expand All @@ -791,9 +790,8 @@ If you are using session middleware, you need to define your session property on
This could look like this:

```ts
const { Telegraf } = require('telegraf')
const session = require('telegraf/session')
const { TelegrafContext } = require('telegraf/typings/context')
import { Context, Telegraf } from 'telegraf'
import session from 'telegraf/session'

interface SessionData {
lastMessageId?: number
Expand All @@ -802,7 +800,7 @@ interface SessionData {
}

// Define your own context type
interface MyContext extends TelegrafContext {
interface MyContext extends Context {
session: SessionData
// ... more props go here
}
Expand All @@ -821,6 +819,7 @@ bot.use((ctx, next) => {
})
bot.on('photo', (ctx, next) => {
ctx.session.photoCount = 1 + (ctx.session.photoCount ?? 0)
next()
KnorpelSenf marked this conversation as resolved.
Show resolved Hide resolved
})
// ...
```
Expand Down