Activities and Workflows as Classes in Typescript SDK

What is it that you would like to inject into your workflows?
We chose functions over classes because those are more idiomatic in the JavaScript ecosystem.
You can implement workflows as classes if you want but you’ll have to build a layer on top of the SDK using the default export and constructing the classes yourself:

import * as workflow from '@temporalio/workflow';

export interface Arg {
  x: number;
}

export class MyWorkflow {
  constructor(private readonly arg: Arg) {
  }
  async execute() {
    await workflow.sleep(this.arg.x);
  }
}

export default async function(...args: unknown[]) {
  const { workflowType } = workflow.workflowInfo();
  if (workflowType === 'MyWorkflow') {
    const instance = new MyWorkflow(...args);
    await instance.execute();
  }
}

You can also use decorators and do automatic binding of signals and queries, I’ll leave that as an exercise for you.

The other (optional) part you’ll need is to wrap the SDK client in your own abstraction that will infer input and output types from your class definitions.

1 Like