Skip to content

Commit

Permalink
fix(types): limit recursion in types using ternary statements (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Nov 22, 2023
1 parent becc4a5 commit 1124632
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/event-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ interface EventHandler<TTransformed> {
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
): void;
onAny(handler: (event: EmitterWebhookEvent & TTransformed) => any): void;
onAny(
handler: (
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed,
) => any,
): void;
onError(
handler: (event: WebhookEventHandlerError<TTransformed>) => any,
): void;
Expand Down
6 changes: 5 additions & 1 deletion src/event-handler/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export function receiverOn(

export function receiverOnAny<TTransformed>(
state: State,
handler: (event: EmitterWebhookEvent & TTransformed) => any,
handler: (
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed,
) => any,
) {
handleEventHandlers(state, "*", handler);
}
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class Webhooks<TTransformed = unknown> {
callback: HandlerFunction<E, TTransformed>,
) => void;
public onAny: (
callback: (event: EmitterWebhookEvent & TTransformed) => any,
callback: (
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed,
) => any,
) => void;
public onError: (
callback: (event: WebhookEventHandlerError<TTransformed>) => any,
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export type WebhookError = Error & Partial<RequestError>;
// todo: rename to "EmitterErrorEvent"
export interface WebhookEventHandlerError<TTransformed = unknown>
extends AggregateError<WebhookError> {
event: EmitterWebhookEvent & TTransformed;
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed;
}

/**
Expand Down

0 comments on commit 1124632

Please sign in to comment.