Skip to content

Type safety on workflow context and metadata via generics #1

Description

@camcima

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions