Skip to content

Commit e7b0eaa

Browse files
committed
Added type safety to the run object page
1 parent f8f21ea commit e7b0eaa

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

docs/realtime/run-object.mdx

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ sidebarTitle: "The run object"
44
description: "The run object schema for Realtime subscriptions"
55
---
66

7-
This is the run object returned by Realtime subscriptions (e.g., `runs.subscribeToRun()`).
7+
The [run object](/realtime/run-object#the-run-object) is the main object returned by Realtime subscriptions (e.g., `runs.subscribeToRun()`). It contains all the information about the run, including the run ID, task identifier, payload, output, and more.
88

9-
## Properties
9+
Type-safety is supported for the run object, so you can infer the types of the run's payload and output. See [type-safety](#type-safety) for more information.
10+
11+
## The run object
12+
13+
### Properties
1014

1115
<ParamField path="id" type="string" required>
1216
The run ID.
@@ -117,20 +121,48 @@ This is the run object returned by Realtime subscriptions (e.g., `runs.subscribe
117121
Indicates whether this is a test run.
118122
</ParamField>
119123

120-
## Type safety
124+
## Type-safety
121125

122-
You can get type safety for the run's payload and output by passing your task type to subscription functions:
126+
You can infer the types of the run's payload and output by passing the type of the task to the `subscribeToRun` function. This will give you type-safe access to the run's payload and output.
123127

124128
```ts
125-
import { runs } from "@trigger.dev/sdk/v3";
129+
import { runs, tasks } from "@trigger.dev/sdk/v3";
126130
import type { myTask } from "./trigger/my-task";
127131

128-
for await (const run of runs.subscribeToRun<typeof myTask>(runId)) {
129-
// run.payload and run.output are now typed based on your task
130-
console.log(run.payload.someProperty); // Type-safe
132+
// Somewhere in your backend code
133+
async function myBackend() {
134+
const handle = await tasks.trigger("my-task", { some: "data" });
131135

132-
if (run.output) {
133-
console.log(run.output.result); // Type-safe
136+
for await (const run of runs.subscribeToRun<typeof myTask>(handle.id)) {
137+
// This will log the run every time it changes
138+
console.log(run.payload.some);
139+
140+
if (run.output) {
141+
// This will log the output if it exists
142+
console.log(run.output.some);
143+
}
144+
}
145+
}
146+
```
147+
148+
When using `subscribeToRunsWithTag`, you can pass a union of task types for all the possible tasks that can have the tag.
149+
150+
```ts
151+
import { runs } from "@trigger.dev/sdk/v3";
152+
import type { myTask, myOtherTask } from "./trigger/my-task";
153+
154+
// Somewhere in your backend code
155+
for await (const run of runs.subscribeToRunsWithTag<typeof myTask | typeof myOtherTask>("my-tag")) {
156+
// You can narrow down the type based on the taskIdentifier
157+
switch (run.taskIdentifier) {
158+
case "my-task": {
159+
console.log("Run output:", run.output.foo); // This will be type-safe
160+
break;
161+
}
162+
case "my-other-task": {
163+
console.log("Run output:", run.output.bar); // This will be type-safe
164+
break;
165+
}
134166
}
135167
}
136168
```

0 commit comments

Comments
 (0)