Skip to main content
zerotal

Documentation


Documentation / @zerotal/flow / index / registerFlowEvent

Function: registerFlowEvent()

registerFlowEvent<K>(name, guard): void

Defined in: flow/src/events.ts:100

Registers a runtime guard for a known event's payload.

Type Parameters

K

K extends never

Parameters

name

K

The known event name to guard (a key of FlowEvents).

guard

(payload) => payload is FlowEvents[K]

A type-guard predicate that narrows an unknown payload to the event's type.

Returns

void

Remarks

The compile-time FlowEvents contract covers dispatch sites at build time; this adds a runtime check for payloads arriving from an untrusted source (a client-originated dispatch) or to catch a refactor the types missed. Opt in per event. When a guard is set, dispatch (and its variants) throw a TypeError if the payload doesn't satisfy it — surfacing a malformed payload loudly instead of letting it flow to listeners. Registering the same event again replaces its guard.

Example

registerFlowEvent("post-created", (p): p is { id: number } =>
  typeof (p as { id?: unknown })?.id === "number");