Type Parameter Issues for Context
#1966
Unanswered
tmercswims
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Do you absolutely need to pass through the whole context? import { Probot, Context } from 'probot';
export = (app: Probot) => {
app.on('pull_request.labeled', onPullRequestLabeled);
app.on('pull_request_review.submitted', onPullRequestReviewed);
};
// attached to a webhook event
async function onPullRequestLabeled(context: Context<'pull_request.labeled'>): Promise<void> {
await checkReadyToMerge(context.payload);
};
// attached to a webhook event
async function onPullRequestReviewed(context: Context<'pull_request_review.submitted'>): Promise<void> {
await checkReadyToMerge(context.payload);
};
// not attached to a webhook event
async function checkReadyToMerge(context: Context<'pull_request' | 'pull_request_review'>['payload']): Promise<boolean> {
// do something
return true;
}; |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This discussion is essentially the same problem as #1680, so I'll try to summarize and simplify as much as I can. The solutions given on that issue are not working for me, and rather than resurrect a closed issue I figured I'd start fresh.
The bot I'm making is in TypeScript. As such, I want/need to use type parameters on
Context
in order for it to have the properties that it should have on itspayload
. Typically that looks something like this:The problem I'm facing is whenever I try to do anything else with the
Context
which implies some difference in the types, even if that difference "should" be allowed. Consider this situation:In this case, I care about two different webhook events, which aren't the same but are similar enough that I can do the same processing to both of them. I've set the types up to support that, as you can see.
However, this does not work. TypeScript gets mad on whichever call of
checkReadyToMerge()
it encounters first during complication, with the following error:If I change things so that type parameters of everything here match exactly, for example make them all
Context<'pull_request.labeled'>
, then that error does not occur. But that isn't what I want, I wantonPullRequestReviewed
to take a context of typeContext<'pull_request_review.submitted'>
. This happens if there is a mismatch in the type parameter of any twoContext
types, assuming that one calls the other or something like that. It even happens if one of them's type parameter is omitted entirely, or is set toany
.I have also tried unioning on multiple
Context
s, each with a single, different type parameter, (Context<'pull_request_review.submitted'> | Context<'pull_request.labeled'>
instead) but the same error occurs when compiling.The same problem also happens if I'm using sub-types of a webhook, like one handler for
pull_request.opened
and another forpull_request.labeled
which both call a helper function which is set up to takeContext<'pull_request'>
. I'd expect either ofpull_request.opened
andpull_request.labeled
to satisfy the type requirement for justpull_request
, and maybe it would, but TypeScript complains with the same complexity error before I can know.I can always get around these issues by strategically casting things to
any
, but I'd really rather not do that if I can at all help it.I'm looking for any advice on what to do here. Thank you!
Versions of relevant things:
Beta Was this translation helpful? Give feedback.
All reactions