grammyjs / emoji

Helpers for sending emojis.
https://grammy.dev/plugins/emoji
MIT License
9 stars 2 forks source link

Feat: Transformative flavour #9

Closed andrrms closed 2 years ago

andrrms commented 2 years ago

Changes made, needs review. If merged, plugin version will go to 2.0.0, as it also changes the name of the flavoring interface. Fix #8

KnorpelSenf commented 2 years ago

This does not fix #8. This PR would need to extract the return type of ctx.reply and use it in ctx.replyWithEmoji.

andrrms commented 2 years ago

Major version change will not be required anymore.

KnorpelSenf commented 2 years ago

Note that you're admin in the repo, there's no need to open pull requests from the fork. You can just open them from other branches in the same repo :)

KnightNiwrem commented 2 years ago

This still does not solve the issue.

The aim is to make the following code compile:

import { Bot } from "https://deno.land/x/grammy/mod.ts";
import {
    hydrate,
    HydrateFlavor,
} from "https://deno.land/x/grammy_hydrate/mod.ts";

type MyContext = EmojiFlavor<HydrateFlavor<Context>>;

const bot = new Bot<MyContext>("");

bot.use(hydrate(), emojiParser(), async (ctx) => {
    const first = await ctx.reply("Ehh");
    await first.delete();
    const second = await ctx.replyWithEmoji`Hi! ${"robot"}`;
    await second.delete();
});

However, I cannot get it to work myself. It seems like ReturnType somehow swallows the changes that the inner transformative context flavour is performing to ctx.reply. Hence, the emoji flavour does not pick this up so it cannot provide the same type signature as ctx.reply has.

I do not see how this can be fixed. Feel free to investigate. Perhaps the hydrate plugin needs to be adjusted, too, who knows.

It doesn't appear to be just ReturnType, but rather a behavior of Typescript, itself. It seems like function type intersections are essentially treated as overloads, and Typescript will simply pick one of them for the return value type (without much consistency).

Typescript Playground

KnorpelSenf commented 2 years ago

It seems like function type intersections are essentially treated as overloads

This is correct, it makes a lot of sense that this is the root cause of the issue.

I'm still unsure how to proceed from here.

KnightNiwrem commented 2 years ago

@andrrms @KnorpelSenf Please take a look at https://github.com/grammyjs/hydrate/pull/1/files if it may be a potential workaround.

KnorpelSenf commented 2 years ago

@andrrms can you please check out https://github.com/grammyjs/hydrate/pull/1 and confirm that everything works for you?