0

I want to create wrapper for node-telegram-bot-api, but typescript is able to resolve only last overload of function TelegramBot.on()

    on(event: 'callback_query', listener: (query: TelegramBot.CallbackQuery) => void): this;

    on(event: 'inline_query', listener: (query: TelegramBot.InlineQuery) => void): this;

    on(event: 'poll_answer', listener: (answer: TelegramBot.PollAnswer) => void): this;

    on(event: 'chat_member' | 'my_chat_member', listener: (member: TelegramBot.ChatMemberUpdated) => void): this;

    on(event: 'chosen_inline_result', listener: (result: TelegramBot.ChosenInlineResult) => void): this;

    on(
        event:
            | 'channel_post'
            | 'edited_message'
            | 'edited_message_text'
            | 'edited_message_caption'
            | 'edited_channel_post'
            | 'edited_channel_post_text'
            | 'edited_channel_post_caption',
        listener: (message: TelegramBot.Message) => void,
    ): this;

    on(event: 'shipping_query', listener: (query: TelegramBot.ShippingQuery) => void): this;

    on(event: 'pre_checkout_query', listener: (query: TelegramBot.PreCheckoutQuery) => void): this;

    on(event: 'polling_error' | 'webhook_error' | 'error', listener: (error: Error) => void): this;

    on(event: 'chat_join_request', listener: (query: TelegramBot.ChatJoinRequest) => void): this;

i created generic type

export type TOnType = TelegramBot['on']
export type Handler<T> = TOnType extends (e:T, a: infer R) => void ? R : never

and it works with

const handler: Handler<'chat_join_request'> = (mes) => 3

but doesn't with

const handler: Handler<'callback_query'> = (mes) => 3

0 Answers0