I'm trying to use Telegraf (4.6.0) types and I'm having an issue with exploring possible message properties.
Here's what I do now:
import { Telegraf, Context } from 'telegraf'
const myBot = new Telegraf(myToken)
listenerBot.on('message', (ctx: Context) => {
const {
text,
forward_from_chat, forward_from_message_id,
photo, caption, caption_entities,
// what it will contain if there's a video? audio? document? ...
} = ctx.message as any
// do stuff with message
}
Since messages can be of various types (in both non-TS and TS sense), when I type ctx.message.
in IDE (VS Code in my case) I'm only suggested the props that are always in message object (like message_id
). Yes, I can do things like
if('text' in ctx.message) {
// do stuff with ctx.message.text
}
but this doesn't help me explore what props can ctx.message
hold. I can imagine a hacky way like
class ExploredContext = ExploreProps<Context> → gives a class similar to Context,
but all possible props are non-optional
...
(ctx as ExploredContext).message._ // cursor here, IDE shows possilbe props
(ctx.message as ExploredMessage)._ // or like this
but neither I know how to implement things like ExploreProps
helper (I'm only aware of utility types) nor I know any better, non-hacky ways to get this (like some configuration of typescript and/or IDE).
Can you suggest a way to implement ExploreProps
or a better way to explore possible props?
(in Telegraf context, I've also asked at in an issue, but a consistent solution would be helpful without regard to Telegraf itself)