[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/flow](../../README) / [index](../README) / registerFlowEvent

# Function: registerFlowEvent()

> **registerFlowEvent**\<`K`\>(`name`, `guard`): `void`

Defined in: [flow/src/events.ts:100](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/flow/src/events.ts#L100)

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](../interfaces/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](../interfaces/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

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