Activities and Workflows as Classes in Typescript SDK

I see in the documentation that Activities and Workflows in the Typescript SDK are always defined as functions. In the Java SDK, they are classes that implement interfaces, which is how I would prefer to work with them for ease of dependency injection, etc. I was wondering if it’s possible to define activities and workflows as classes in Typescript with dependency injection through the constructor? What is the reason for them being functions instead of classes? Further, can workers be implemented as classes with interfaces rather than functions, or does the Typescript SDK not support that either?

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