Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): resolve correct value for BaseWebhookEvent#name #435

Merged
merged 1 commit into from
Feb 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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