Created
November 24, 2021 13:57
-
-
Save FatalMerlin/fb79cb0b9d063ea4b6dff2460e403cd1 to your computer and use it in GitHub Desktop.
grammY ctx.api.config.use payload type-check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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