Skip to content

@temporal-contract/contract


@temporal-contract/contract

Type Aliases

ActivityDefinition

ts
type ActivityDefinition<TInput, TOutput> = object;

Defined in: types.ts:13

Definition of an activity

Type Parameters

Type ParameterDefault type
TInput extends AnySchemaAnySchema
TOutput extends AnySchemaAnySchema

Properties

PropertyModifierTypeDefined in
inputreadonlyTInputtypes.ts:17
outputreadonlyTOutputtypes.ts:18

AnySchema

ts
type AnySchema = StandardSchemaV1;

Defined in: types.ts:8

Base types for validation schemas Any schema that implements the Standard Schema specification This includes Zod, Valibot, ArkType, and other compatible libraries


ContractDefinition

ts
type ContractDefinition<TWorkflows, TActivities> = object;

Defined in: types.ts:70

Contract definition containing workflows and optional global activities

Type Parameters

Type ParameterDefault type
TWorkflows extends Record<string, WorkflowDefinition>Record<string, WorkflowDefinition>
TActivities extends Record<string, ActivityDefinition>Record<string, ActivityDefinition>

Properties

PropertyModifierTypeDefined in
activities?readonlyTActivitiestypes.ts:76
taskQueuereadonlystringtypes.ts:74
workflowsreadonlyTWorkflowstypes.ts:75

InferActivityNames

ts
type InferActivityNames<TContract> = TContract["activities"] extends Record<string, ActivityDefinition> ? keyof TContract["activities"] & string : never;

Defined in: types.ts:104

Extract activity names from a contract (global activities) as a union type

Type Parameters

Type Parameter
TContract extends ContractDefinition

Example

typescript
type MyActivityNames = InferActivityNames<typeof myContract>;
// "log" | "sendEmail"

InferContractWorkflows

ts
type InferContractWorkflows<TContract> = TContract["workflows"];

Defined in: types.ts:117

Extract all workflows from a contract with their definitions

Type Parameters

Type Parameter
TContract extends ContractDefinition

Example

typescript
type MyWorkflows = InferContractWorkflows<typeof myContract>;

InferWorkflowNames

ts
type InferWorkflowNames<TContract> = keyof TContract["workflows"] & string;

Defined in: types.ts:92

Extract workflow names from a contract as a union type

Type Parameters

Type Parameter
TContract extends ContractDefinition

Example

typescript
type MyWorkflowNames = InferWorkflowNames<typeof myContract>;
// "processOrder" | "sendNotification"

QueryDefinition

ts
type QueryDefinition<TInput, TOutput> = object;

Defined in: types.ts:31

Definition of a query

Type Parameters

Type ParameterDefault type
TInput extends AnySchemaAnySchema
TOutput extends AnySchemaAnySchema

Properties

PropertyModifierTypeDefined in
inputreadonlyTInputtypes.ts:35
outputreadonlyTOutputtypes.ts:36

SignalDefinition

ts
type SignalDefinition<TInput> = object;

Defined in: types.ts:24

Definition of a signal

Type Parameters

Type ParameterDefault type
TInput extends AnySchemaAnySchema

Properties

PropertyModifierTypeDefined in
inputreadonlyTInputtypes.ts:25

UpdateDefinition

ts
type UpdateDefinition<TInput, TOutput> = object;

Defined in: types.ts:42

Definition of an update

Type Parameters

Type ParameterDefault type
TInput extends AnySchemaAnySchema
TOutput extends AnySchemaAnySchema

Properties

PropertyModifierTypeDefined in
inputreadonlyTInputtypes.ts:46
outputreadonlyTOutputtypes.ts:47

WorkflowDefinition

ts
type WorkflowDefinition<TActivities, TSignals, TQueries, TUpdates> = object;

Defined in: types.ts:53

Definition of a workflow

Type Parameters

Type ParameterDefault type
TActivities extends Record<string, ActivityDefinition>Record<string, ActivityDefinition>
TSignals extends Record<string, SignalDefinition>Record<string, SignalDefinition>
TQueries extends Record<string, QueryDefinition>Record<string, QueryDefinition>
TUpdates extends Record<string, UpdateDefinition>Record<string, UpdateDefinition>

Properties

PropertyModifierTypeDefined in
activities?readonlyTActivitiestypes.ts:61
inputreadonlyAnySchematypes.ts:59
outputreadonlyAnySchematypes.ts:60
queries?readonlyTQueriestypes.ts:63
signals?readonlyTSignalstypes.ts:62
updates?readonlyTUpdatestypes.ts:64

Functions

defineActivity()

ts
function defineActivity<TActivity>(definition): TActivity;

Defined in: builder.ts:43

Define a Temporal activity with type-safe input and output schemas.

Activities are the building blocks of Temporal workflows that execute business logic and interact with external services. This function preserves TypeScript types while providing a consistent structure for activity definitions.

Type Parameters

Type ParameterDescription
TActivity extends ActivityDefinitionThe activity definition type with input/output schemas

Parameters

ParameterTypeDescription
definitionTActivityThe activity definition containing input and output schemas

Returns

TActivity

The same definition with preserved types for type inference

Example

typescript
import { defineActivity } from '@temporal-contract/contract';
import { z } from 'zod';

export const sendEmail = defineActivity({
  input: z.object({
    to: z.string().email(),
    subject: z.string(),
    body: z.string(),
  }),
  output: z.object({
    messageId: z.string(),
    sentAt: z.date(),
  }),
});

defineContract()

ts
function defineContract<TContract>(definition): TContract;

Defined in: builder.ts:226

Define a complete Temporal contract with type-safe workflows and activities.

A contract is the central definition that ties together your Temporal application's workflows and activities. It provides:

  • Type safety across client, worker, and workflow code
  • Automatic validation at runtime
  • Compile-time verification of implementations
  • Clear API boundaries and documentation

The contract validates the structure and ensures:

  • Task queue is specified
  • At least one workflow is defined
  • Valid JavaScript identifiers are used
  • No conflicts between global and workflow-specific activities
  • All schemas implement the Standard Schema specification

Type Parameters

Type ParameterDescription
TContract extends ContractDefinitionThe contract definition type

Parameters

ParameterTypeDescription
definitionTContractThe complete contract definition

Returns

TContract

The same definition with preserved types for type inference

Throws

If the contract structure is invalid

Example

typescript
import { defineContract } from '@temporal-contract/contract';
import { z } from 'zod';

export const myContract = defineContract({
  taskQueue: 'orders',
  workflows: {
    processOrder: {
      input: z.object({ orderId: z.string() }),
      output: z.object({ success: z.boolean() }),
      activities: {
        chargePayment: {
          input: z.object({ amount: z.number() }),
          output: z.object({ transactionId: z.string() }),
        },
      },
    },
  },
  // Optional global activities shared across workflows
  activities: {
    logEvent: {
      input: z.object({ message: z.string() }),
      output: z.void(),
    },
  },
});

defineQuery()

ts
function defineQuery<TQuery>(definition): TQuery;

Defined in: builder.ts:100

Define a Temporal query with type-safe input and output schemas.

Queries allow you to read the current state of a running workflow without modifying it. They are synchronous and should not perform any mutations.

Type Parameters

Type ParameterDescription
TQuery extends QueryDefinitionThe query definition type with input/output schemas

Parameters

ParameterTypeDescription
definitionTQueryThe query definition containing input and output schemas

Returns

TQuery

The same definition with preserved types for type inference

Example

typescript
import { defineQuery } from '@temporal-contract/contract';
import { z } from 'zod';

export const getOrderStatus = defineQuery({
  input: z.object({ orderId: z.string() }),
  output: z.object({
    status: z.enum(['pending', 'processing', 'completed', 'failed']),
    updatedAt: z.date(),
  }),
});

defineSignal()

ts
function defineSignal<TSignal>(definition): TSignal;

Defined in: builder.ts:72

Define a Temporal signal with type-safe input schema.

Signals are asynchronous messages sent to running workflows to update their state or trigger certain behaviors. This function ensures type safety for signal payloads.

Type Parameters

Type ParameterDescription
TSignal extends SignalDefinitionThe signal definition type with input schema

Parameters

ParameterTypeDescription
definitionTSignalThe signal definition containing input schema

Returns

TSignal

The same definition with preserved types for type inference

Example

typescript
import { defineSignal } from '@temporal-contract/contract';
import { z } from 'zod';

export const approveOrder = defineSignal({
  input: z.object({
    orderId: z.string(),
    approvedBy: z.string(),
  }),
});

defineUpdate()

ts
function defineUpdate<TUpdate>(definition): TUpdate;

Defined in: builder.ts:132

Define a Temporal update with type-safe input and output schemas.

Updates are similar to signals but return a value and wait for the workflow to process them before completing. They provide a synchronous way to modify workflow state and get immediate feedback.

Type Parameters

Type ParameterDescription
TUpdate extends UpdateDefinitionThe update definition type with input/output schemas

Parameters

ParameterTypeDescription
definitionTUpdateThe update definition containing input and output schemas

Returns

TUpdate

The same definition with preserved types for type inference

Example

typescript
import { defineUpdate } from '@temporal-contract/contract';
import { z } from 'zod';

export const updateOrderQuantity = defineUpdate({
  input: z.object({
    orderId: z.string(),
    newQuantity: z.number().positive(),
  }),
  output: z.object({
    success: z.boolean(),
    totalPrice: z.number(),
  }),
});

defineWorkflow()

ts
function defineWorkflow<TWorkflow>(definition): TWorkflow;

Defined in: builder.ts:169

Define a Temporal workflow with type-safe input, output, and associated operations.

Workflows are durable functions that orchestrate activities, handle timeouts, and manage long-running processes. This function provides type safety for the entire workflow definition including activities, signals, queries, and updates.

Type Parameters

Type ParameterDescription
TWorkflow extends WorkflowDefinitionThe workflow definition type with all associated schemas

Parameters

ParameterTypeDescription
definitionTWorkflowThe workflow definition containing input, output, and operations

Returns

TWorkflow

The same definition with preserved types for type inference

Example

typescript
import { defineWorkflow, defineActivity, defineSignal } from '@temporal-contract/contract';
import { z } from 'zod';

export const processOrder = defineWorkflow({
  input: z.object({ orderId: z.string() }),
  output: z.object({ success: z.boolean() }),
  activities: {
    validatePayment: defineActivity({
      input: z.object({ orderId: z.string() }),
      output: z.object({ valid: z.boolean() }),
    }),
  },
  signals: {
    cancel: defineSignal({
      input: z.object({ reason: z.string() }),
    }),
  },
});

Released under the MIT License.