Skip to content

Instantly share code, notes, and snippets.

@FatalMerlin
Created November 24, 2021 13:57
Show Gist options
  • Save FatalMerlin/fb79cb0b9d063ea4b6dff2460e403cd1 to your computer and use it in GitHub Desktop.
Save FatalMerlin/fb79cb0b9d063ea4b6dff2460e403cd1 to your computer and use it in GitHub Desktop.
grammY ctx.api.config.use payload type-check
ctx.api.config.use((prev, method, payload) => {
if (isPayloadOfType(payload, method, 'sendMessage')) {
// payload is now of type Payload<'sendMessage', R>
payload.reply_markup = {
...payload.reply_markup,
keyboard: numberKeyboard.build(),
};
}
return prev(method, payload);
});
const isPayloadOfType = <R extends RawApi, Q extends Methods<R>, M extends Q, T extends Q>(
payload: Payload<M, R>,
method: M,
desired: T,
): payload is Payload<T, R> => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return method === desired;
};
// Usage
if (isPayloadOfType(payload, method, 'sendMessage')) {
// payload is now of type Payload<'sendMessage', R>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment