grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.21k stars 110 forks source link

Type issue #432

Closed mikiyasET closed 1 year ago

mikiyasET commented 1 year ago

Error message

TS2345: Argument of type '{ reply_markup: { remove_keyboard: boolean; }; }' is not assignable to parameter of type 'Other<"sendMessage", "text" | "chat_id">'.
  Types of property 'reply_markup' are incompatible.
    Type '{ remove_keyboard: boolean; }' is not assignable to type 'InlineKeyboardMarkup | ReplyKeyboardMarkup | ReplyKeyboardRemove | ForceReply | undefined'.
      Type '{ remove_keyboard: boolean; }' is not assignable to type 'ReplyKeyboardRemove'.
        Types of property 'remove_keyboard' are incompatible.
          Type 'boolean' is not assignable to type 'true'.

it works when I ignored it with // @ts-ignore

image
KnorpelSenf commented 1 year ago

Show the code

rojvv commented 1 year ago

@mikiyasET Did you accidentally pass something other than true to the remove_keyboard parameter?

Then you shouldn’t because it won’t have any effects if you pass something other than true.

If you are currently doing something like this:

await ctx.reply("text", { reply_markup: { remove_keyboard: shouldRemoveKeyboard } });

where shouldRemoveKeyboard is a boolean.

You should make it so it looks like this:

await ctx.reply("text", { reply_markup: shouldRemoveKeyboard ? { remove_keyboard: true } : undefined });

Or like this:

const text = "text";

if (shouldRemoveKeyboard) {
    await ctx.reply(text, { reply_markup: { remove_keyboard: true } 
} else {});
    await ctx.reply(text);
}
KnorpelSenf commented 1 year ago

Or how about function f(remove?: true) { // etc or just const remove: true | undefined?

mikiyasET commented 1 year ago

@roj1512

await ctx.reply(text, { reply_markup: { remove_keyboard: true } });

This is what i used tho the { reply_markup: { remove_keyboard: true } } is within a function, and I'm calling the function as I said it works when ignoring the type

KnorpelSenf commented 1 year ago

The function that you're using has the wrong type annotation. If you can't make it work based on our suggestions, you need to share your exact code so we can tell you how to correct it.

Also, note that suppressing compiler errors is rarely a good way to fix problems—they exist for a good reason and you should just fix your code :)

mikiyasET commented 1 year ago

here is the function

removeKYB = () => {
        return {
            reply_markup: {
                remove_keyboard: true,
            }
        }
    }

and here is where the type error occurred

await ctx.reply("My Message", removeKYB);

if I ignore it using // @ts-ignore or give the removeKYB function an any type it won't show any type issues. The reason I posted it here as an issue is it works fine with other keyboards and everything but when remove_keyboard: true, is add to the reply_markup the type issue will come and I think there is a type issue on the package can any Contributor look in to this please.

KnorpelSenf commented 1 year ago

Use remove_keyboard: true as const

KnorpelSenf commented 1 year ago

Closing because of inactivity. This is fixed anyway, the types are correct.