Summary
Both context and metadata are currently typed as Record<string, unknown>, which means every command handler has to do unsafe casts like ctx.metadata.orderId as string. This leaves TypeScript's strongest feature on the table.
Proposal
Introduce a generic WorkflowDefinition<TContext, TMetadata> that flows through to WorkflowCommand<TSubject, TContext, TMetadata>, giving compile-time safety on what keys exist in context and metadata.
Benefits
- Compile-time validation of context/metadata key access
- IDE autocompletion for context and metadata properties
- Eliminates
as string / as number casts in command handlers
- Significant differentiator — most workflow engines in any language don't offer typed workflow context
Considerations
- Adds complexity to the definition registry and compilation
- Need to consider how generics interact with the workflow instance persistence layer (serialization/deserialization)
Summary
Both
contextandmetadataare currently typed asRecord<string, unknown>, which means every command handler has to do unsafe casts likectx.metadata.orderId as string. This leaves TypeScript's strongest feature on the table.Proposal
Introduce a generic
WorkflowDefinition<TContext, TMetadata>that flows through toWorkflowCommand<TSubject, TContext, TMetadata>, giving compile-time safety on what keys exist in context and metadata.Benefits
as string/as numbercasts in command handlersConsiderations