Skip to content

Commit

Permalink
fix(typescript): resolve correct value for BaseWebhookEvent#name
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Feb 3, 2021
1 parent a4335d0 commit b59a12a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ export type EmitterEvent = EmitterEventMap[EmitterEventName];

export type EmitterAnyEvent = EmitterWebhookEventMap["*"];

export type ToWebhookEvent<
TEmitterEvent extends string
> = TEmitterEvent extends `${infer TWebhookEvent}.${string}`
? TWebhookEvent
: TEmitterEvent;

interface BaseWebhookEvent<
TName extends keyof EmitterEventPayloadMap = keyof EmitterEventPayloadMap
> {
id: string;
name: TName;
name: ToWebhookEvent<TName>;
payload: EmitterEventPayloadMap[TName];
}

Expand Down
26 changes: 25 additions & 1 deletion test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const fn = (webhookEvent: EmitterWebhookEvent) => {
console.log(webhookEvent.payload.sender);
}
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"*" | "check_run" | ... many more ... | "workflow_run"' and '"check_run.completed"' have no overlap.
if (webhookEvent.name === "check_run.completed") {
console.log(webhookEvent.payload.action);
//
}

if (webhookEvent.name === "*") {
Expand Down Expand Up @@ -170,6 +172,28 @@ export default async function () {
}
});

webhooks.on(["check_run.completed", "code_scanning_alert.fixed"], (event) => {
if (event.name === "check_run") {
console.log(`a run was ${event.payload.action}!`);

if (event.payload.action === "completed") {
console.log("it was the completed action, obviously!");
}

// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"completed"' and '"created"' have no overlap.
if (event.payload.action === "created") {
//
}
}

// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"check_run" | "code_scanning_alert"' and '"repository"' have no overlap.
if (event.name === "repository") {
//
}
});

webhooks.on("issues", (event) => {
console.log(event.payload.issue);
});
Expand Down

0 comments on commit b59a12a

Please sign in to comment.