Repository navigation
Closed
Labels
✅ accepted-PRs-welcomeFeature proposal is accepted and ready to work on📚 documentation / examplesImprovements or additions to documentation
Description
Area of Improvement
I noticed that the docs aren't up to date (or don't consider) using zod v4 and subscriptions. The zAsyncIterable would need to look smth like this as of the updated z.ZodType
import { isTrackedEnvelope, tracked, TrackedEnvelope } from "@trpc/server";
import { z } from "zod";
function isAsyncIterable<TValue, TReturn = unknown>(
value: unknown,
): value is AsyncIterable<TValue, TReturn> {
return (
!!value && typeof value === "object" && Symbol.asyncIterator in value
);
}
const trackedEnvelopeSchema =
z.custom<TrackedEnvelope<unknown>>(isTrackedEnvelope);
/**
* A Zod schema helper designed specifically for validating async iterables. This schema ensures that:
* 1. The value being validated is an async iterable.
* 2. Each item yielded by the async iterable conforms to a specified type.
* 3. The return value of the async iterable, if any, also conforms to a specified type.
*/
export function zAsyncIterable<
TYieldIn,
TYieldOut,
TReturnIn = void,
TReturnOut = void,
Tracked extends boolean = false,
>(opts: {
/**
* Validate the value yielded by the async generator
*/
yield: z.ZodType<TYieldOut, TYieldIn>;
/**
* Validate the return value of the async generator
* @remark not applicable for subscriptions
*/
return?: z.ZodType<TReturnOut, TReturnIn>;
/**
* Whether if the yielded values are tracked
* @remark only applicable for subscriptions
*/
tracked?: Tracked;
}) {
return z
.custom<
AsyncIterable<
Tracked extends true ? TrackedEnvelope<TYieldIn> : TYieldIn,
TReturnIn
>
>((val) => isAsyncIterable(val))
.transform(async function* (iter) {
const iterator = iter[Symbol.asyncIterator]();
try {
let next;
while ((next = await iterator.next()) && !next.done) {
if (opts.tracked) {
const [id, data] = trackedEnvelopeSchema.parse(
next.value,
);
yield tracked(id, await opts.yield.parseAsync(data));
continue;
}
yield opts.yield.parseAsync(next.value);
}
if (opts.return) {
return await opts.return.parseAsync(next.value);
}
return;
} finally {
await iterator.return?.();
}
}) as z.ZodType<
AsyncIterable<
Tracked extends true ? TrackedEnvelope<TYieldIn> : TYieldIn,
TReturnIn
>,
AsyncIterable<
Tracked extends true ? TrackedEnvelope<TYieldOut> : TYieldOut,
TReturnOut
>
>;
}Link to related docs
No response
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing the suggested changes!
Reacted by Wei FeiYang and Jack Stevenson
Reacted by Jack Stevenson, Daan Boerlage and Paul Ostarek
Reacted by Jack Stevenson
Metadata
Metadata
Assignees
Labels
✅ accepted-PRs-welcomeFeature proposal is accepted and ready to work on📚 documentation / examplesImprovements or additions to documentation
Activity
Wesley-Young commentedon Oct 11, 2025
This helped me a lot when I am upgrading my code👍
chancegraff commentedon Feb 12, 2026
Full TypeScript compatible version without
ascasting (Tracked might not work fully in this version):cogwirrel commentedon Feb 19, 2026
Thanks for this! Do you think there'd be scope to export this helper as part of tRPC? Would be handy not to have to copy/paste :)
docs: fix zod v4 zAsyncIterable generic order in subscriptions guide (#…
github-actions commentedon Mar 1, 2026
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.