# Types

## Context Types

Context types define what properties and methods are available in different parts of the actor lifecycle.

examples/docs/actors-types/context-types.ts:

```ts
import { actor } from "rivetkit";

const counter = actor({
  // CreateContext in createState hook
  createState: (c, input: { initial: number }): { count: number } => {
    return { count: input.initial };
  },

  // ActionContext in actions
  actions: {
    increment: (c) => {
      c.state.count += 1;
      return c.state.count;
    }
  }
});
```

### Extracting Context Types

When writing helper functions that work with actor contexts, use context extractor types like `CreateContextOf` or `ActionContextOf` to extract the appropriate context type from your actor definition.

examples/docs/actors-types/extracting-context-types.ts:

```ts
import { actor, CreateContextOf, ActionContextOf } from "rivetkit";

const gameRoom = actor({
  createState: (c, input: { roomId: string }): { players: string[]; score: number } => {
    initializeRoom(c, input.roomId);
    return { players: [] as string[], score: 0 };
  },

  actions: {
    addPlayer: (c, playerId: string) => {
      validatePlayer(c, playerId);
      c.state.players.push(playerId);
    }
  }
});

// Extract CreateContext type for createState hook
function initializeRoom(
  context: CreateContextOf<typeof gameRoom>,
  roomId: string
) {
  console.log(`Initializing room: ${roomId}`);
  // context.state is not available here (being created)
  // context.vars is not available here (not created yet)
}

// Extract ActionContext type for actions
function validatePlayer(
  context: ActionContextOf<typeof gameRoom>,
  playerId: string
) {
  // Full context available in actions
  if (context.state.players.includes(playerId)) {
    throw new Error("Player already in room");
  }
}
```

### All Context Types

Each lifecycle hook and handler has a corresponding `*ContextOf` type, exported from `"rivetkit"`. Pass `typeof myActor` as the type parameter.

| Hook / Handler | Context Type |
|---|---|
| `createState` | `CreateContextOf` |
| `onCreate` | `CreateContextOf` |
| `createVars` | `CreateVarsContextOf` |
| `createConnState` | `CreateConnStateContextOf` |
| `onBeforeConnect` | `BeforeConnectContextOf` |
| `onConnect` | `ConnectContextOf` |
| `onDisconnect` | `DisconnectContextOf` |
| `onDestroy` | `DestroyContextOf` |
| `onMigrate` | `MigrateContextOf` |
| `onWake` | `WakeContextOf` |
| `onSleep` | `SleepContextOf` |
| `onStateChange` | `StateChangeContextOf` |
| `onBeforeActionResponse` | `BeforeActionResponseContextOf` |
| `actions.*` | `ActionContextOf` |
| `run` | `RunContextOf` |
| `workflow` orchestration helpers (root, `loop`, `try`, `race`, `join` branches) | `WorkflowContextOf` |
| `workflow` `step` / `tryStep` run + rollback helpers | `WorkflowStepContextOf` |
| `onRequest` | `RequestContextOf` |
| `onWebSocket` | `WebSocketContextOf` |

`ActorContextOf`, `ConnContextOf`, and `ConnInitContextOf` are general-purpose base context types useful for helper functions that don't correspond to a specific hook.

Workflow context extractors are exported from both `"rivetkit"` and `"rivetkit/workflow"`.
