createTypedPubSub()
createTypedPubSub<
Engine
,Map
>(pubsub
,options?
):TypedPubSub
<Engine
,Map
>
Creates a type-safe wrapper around a PubSub implementation.
This utility ensures that your subscription channels and their payloads are properly typed, helping catch potential errors at compile time.
Type Parameters
Type Parameter |
---|
|
|
Parameters
Parameter | Type | Description |
---|---|---|
|
|
The PubSub engine instance (e.g., PubSub, RedisPubSub) |
|
Configuration options |
Returns
TypedPubSub
<Engine
, Map
>
Example
// Define your event map
type PubSubMap = {
"user-updated": User;
[c: `user-updated-${string}`]: User;
};
// Create typed PubSub instance
const pubsub = createTypedPubSub<PubSub, PubSubMap>(new PubSub());
// Usage with Redis
const pubsub = createTypedPubSub<RedisPubSub, PubSubMap>(
new RedisPubSub({
publisher: new Redis(options),
subscriber: new Redis(options),
}),
{
prefix: "feature-1:",
},
);