Domain events with discriminated union
type DomainEvent =
| { type: 'MedicineAssigned'; patientId: string; medicineId: string }
| { type: 'TaskCompleted'; taskId: string; completedAt: Date }
| { type: 'UserRegistered'; userId: string; email: string }
const outbox = createOutboxConsumer<DomainEvent>({
// ...
publish: async (event) => {
switch (event.type) {
case 'MedicineAssigned':
// TypeScript knows event has patientId and medicineId
await handleMedicineAssignment(event)
break
case 'TaskCompleted':
// TypeScript knows event has taskId and completedAt
await handleTaskCompletion(event)
break
case 'UserRegistered':
// TypeScript knows event has userId and email
await handleUserRegistration(event)
break
}
}
})
Base type for all events.
Events can be any object type. Use discriminated unions with a
typefield for type-safe event handling.