Skip to content

Type Alias: HookableEvents<T>

ts
type HookableEvents<T> = {
  [K in keyof T]: T[K] extends (args: infer P) => boolean | void
    ? [callbackArgs: [...P], cleanupCallbackArgs: [error: Error | null, ...P]]
    : never;
};

Transforms emit validators or emit functions into hookable event types.

Each event gets converted to a tuple with two signatures:

  1. Hook handler signature - receives the original event parameters
  2. Cleanup function signature - receives Error | null plus the original parameters

Type Parameters

Type ParameterDescription
T extends EmitValidators | EmitFunctions<EmitValidators>The emit validators or emit functions object type

Example

typescript
const emits = {
  saving: (model: BaseModel) => true,
  finding: (query: QueryBuilder) => true,
};

type Events = HookableEvents<typeof emits>;
// {
//   saving: [
//     [BaseModel],                      // for hook handler
//     [error: Error | null, BaseModel], // for cleanup function
//   ],
//   finding: [
//     [QueryBuilder],                      // for hook handler
//     [error: Error | null, QueryBuilder], // for cleanup function
//   ]
// }