grammyjs / grammY

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

Need workaround for filter function overload's type inference #395

Open KnightNiwrem opened 1 year ago

KnightNiwrem commented 1 year ago
yarn add @grammyjs/ratelimiter
yarn add https://github.com/grammyjs/grammY.git
import { Bot, Context } from 'grammy';
import { limit } from '@grammyjs/ratelimiter';

type FromContext = Context & { from: NonNullable<Context['from']> };

const bot = new Bot('');
const MyC = bot.filter((ctx): ctx is FromContext=> !!ctx.from);
MyC.filter(
  ctx => true,
  limit({ onLimitExceeded: ctx => ctx.answerCallbackQuery() }),
);
MyC.filter(
  ctx => true,
  ctx => ctx.from.first_name,
);
// In node_modules > grammy > out > composer.d.ts
// swap the ordering of the following lines to toggle different errors
filter<D extends C>(predicate: (ctx: C) => ctx is D, ...middleware: Array<Middleware<D>>): Composer<D>;
filter(predicate: (ctx: C) => MaybePromise<boolean>, ...middleware: Array<Middleware<C>>): Composer<C>;
KnorpelSenf commented 1 year ago

Context: this is caused by a bug in TypeScript. It would be nice if we found a workaround so we don't have to wait for Microsoft to fix it.

Also, we should probably open an issue about this.