Sign In
Lifecycle & Config

Helper Types

Rivet provides several TypeScript helper types to make it easier to work with actors in a type-safe way.

Context Types

When working with actors, you often need to access the context object outside of the actor's handlers. Rivet provides helper types to extract the context types from actor definitions.

ActorContextOf<ActorDefinition>

Extracts the full actor context type from an actor definition. This is the type of the context object (c) available in lifecycle hooks and in actions.

TypeScript
import { actor, ActorContextOf } from "@rivetkit/actor";

const chatRoom = actor({
  state: { messages: [] },
  actions: {
  onStart: (c, message) => {
    // Can ActorContext be used in lifecycle hooks
    exampleFunction(c, "Actor started");
  },
  actions: {
    sendMessage: (c, message) => {
      // Can ActorContext also be used in actions
      processChatRoomContext(c, mssage);
    }
  }
});

// Now you can use this type elsewhere
function logMessage(context: ActorContextOf<typeof chatRoom>, message: string) {
  console.log(context.state.messages);
  context.broadcast("newEvent", { type: "system", message });
}

ActionContextOf<ActorDefinition>

Extracts the action context type from an actor definition. This is the type of the context object (c) available in actions. This cannot be used in lifecycle hooks.

TypeScript
import { actor, ActionContextOf } from "@rivetkit/actor";

const counterWithProcessing = actor({
  state: { count: 0 },
  actions: {
    increment: (c) => {
      // Use our helper function to process the context
      processCounterAction(c);
      return c.state.count;
    }
  }
});

function processCounterAction(context: ActionContextOf<typeof counter>) {
  context.state.count++;
}
Suggest changes to this page