electric-sql / electric-sql/electric

Improve typing for wake event payloads

Open
#4,518 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
10.4k
Forks
375
Avg merge
3d 1h
Merged PRs (30d)
18

Description

## Problem

`WakeEvent` currently exposes `payload?: unknown`, so handlers that react to specific wake kinds have to use ad-hoc casts before accessing structured payloads.

This came up in `examples/agents-walkthrough`, where child-completion wakes carry a payload like:

```ts
{
finished_child: FinishedChild
}
```

But the handler can only see `wake.payload?: unknown`, so precise code either repeats casts or uses optional/defensive access:

```ts
const finishedChild = (
wake.payload as { finished_child?: FinishedChild } | undefined
)?.finished_child
```

That avoids type errors, but it is not ideal: it obscures the contract for child-completion wakes and makes examples less clear.

## Desired direction

Provide stronger / discriminated typing for wake events and wake payloads, especially for runtime-generated wake kinds such as child run completion.

Possible approaches:

- Make `WakeEvent` a discriminated union for known runtime wake kinds.
- Export canonical payload types, e.g. `FinishedChildWakePayload` / `ChildRunFinishedWakeEvent`.
- Provide helper/narrowing functions, e.g. `isFinishedChildWake(wake)` or `getFinishedChild(wake)`.
- Ensure examples and docs can access structured wake payloads without local casts.

## Acceptance criteria

- Child-completion wake handlers can access `finished_child` with precise types.
- Existing generic/custom wake payload use cases remain supported.
- Examples avoid repeated `as { ... }` casts for known runtime wake payloads.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.