Skip to content

docs: fix zod v4 zAsyncIterable generic order in subscriptions guide - #7174

Merged
Nick-Lucas merged 2 commits into
trpc:mainfrom
aviu16:codex/docs-zasynciterable-zod4
Feb 28, 2026
Merged

Nick-Lucas merged 2 commits into
trpc:mainfrom
aviu16:codex/docs-zasynciterable-zod4

Conversation

@aviu16

@aviu16 aviu16 commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix the zod v4 output-validation snippet in the subscriptions docs to use the correct ZodType<Output, Input> generic order.

What changed

  • In /www/docs/server/subscriptions.md (zod v4 section):
    • yield changed from z.ZodType<TYieldIn, TYieldOut> to z.ZodType<TYieldOut, TYieldIn>
    • return changed from z.ZodType<TReturnIn, TReturnOut> to z.ZodType<TReturnOut, TReturnIn>

Why

ZodType in v4 is parameterized as <Output, Input>. The previous order inverted input/output in the example and was confusing when adapting the helper.

Closes #6952

@aviu16
aviu16 requested a review from a team as a code owner February 20, 2026 02:31
@vercel

vercel Bot commented Feb 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
next-prisma-starter Ready Ready Preview Feb 28, 2026 0:27am
og-image Ready Ready Preview, Comment Feb 28, 2026 0:27am

Request Review

@coderabbitai

coderabbitai Bot commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 948fa29 and 8770e21.

📒 Files selected for processing (2)
  • packages/tests/server/zAsyncIterable.ts
  • www/docs/server/subscriptions.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/tests/server/zAsyncIterable.ts
  • www/docs/server/subscriptions.md

Walkthrough

Swapped the ZodType generic parameter ordering for the yield and return validators in zAsyncIterable option typings to match Zod v4's ZodType<TOutput, TInput> ordering. No runtime or control-flow changes were made.

Changes

Cohort / File(s) Summary
Docs: zAsyncIterable signature
www/docs/server/subscriptions.md
Updated zAsyncIterable options: yield changed from z.ZodType<TYieldIn, TYieldOut> to z.ZodType<TYieldOut, TYieldIn>; return changed from z.ZodType<TReturnIn, TReturnOut> to z.ZodType<TReturnOut, TReturnIn>.
Tests: zAsyncIterable typing
packages/tests/server/zAsyncIterable.ts
Aligned test typings with docs change: swapped generic parameter order for yield and return z.ZodType declarations to match Zod v4 ordering.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped through types with nimble cheer,

swapped two pairs so the order's clear.
Zod v4's tune now hums in place,
docs and tests in tidy grace. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description explains what changed and why, but lacks explicit checklist completion and is missing structure from the provided template. Complete the checklist section and explicitly confirm that documentation and related tests have been updated.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing zod v4 generic parameter order in the zAsyncIterable documentation example.
Linked Issues check ✅ Passed The PR updates documentation to reflect zod v4's ZodType<Output, Input> ordering and mirrors changes in the test helper, addressing the core requirements from issue #6952.
Out of Scope Changes check ✅ Passed All changes are narrowly scoped to correcting the generic type parameter order in zAsyncIterable examples and align with the linked issue requirements.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@www/docs/server/subscriptions.md`:
- Around line 275-280: The generics for the Zod types are inverted: z.ZodType is
<Output, Input> so swap the type parameters for the yield and return validators
to match the final cast and runtime parse calls; change the declarations of
yield and return from z.ZodType<TYieldOut, TYieldIn> and z.ZodType<TReturnOut,
TReturnIn> to z.ZodType<TYieldIn, TYieldOut> and z.ZodType<TReturnIn,
TReturnOut> respectively so that opts.yield.parseAsync receives a TYieldOut and
yields TYieldIn, and opts.return aligns with the AsyncIterable cast and
transform logic.

Comment thread www/docs/server/subscriptions.md Resolved
@pkg-pr-new

pkg-pr-new Bot commented Feb 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@trpc/client

npm i https://pkg.pr.new/@trpc/client@7174

@trpc/next

npm i https://pkg.pr.new/@trpc/next@7174

@trpc/react-query

npm i https://pkg.pr.new/@trpc/react-query@7174

@trpc/server

npm i https://pkg.pr.new/@trpc/server@7174

@trpc/tanstack-react-query

npm i https://pkg.pr.new/@trpc/tanstack-react-query@7174

@trpc/upgrade

npm i https://pkg.pr.new/@trpc/upgrade@7174

commit: e8214dc

@aviu16

aviu16 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

Maintenance update: this docs fix is ready from my side. If you want, I can rebase/refresh to current base branch right away. The change is intentionally minimal (only the zod v4 generic order in the subscriptions guide).

@KATT KATT left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test file where this is copied from, can you update both?

@aviu16
aviu16 requested a review from a team as a code owner February 25, 2026 15:37
@aviu16

aviu16 commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

@KATT Done. I updated the mirrored test helper file as requested (packages/tests/server/zAsyncIterable.ts) so both doc and test copy now use the same zod v4 generic order.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
packages/tests/server/zAsyncIterable.ts (2)

53-60: ⚠️ Potential issue | 🔴 Critical

Pre-existing bug: missing await on yield in the non-tracked path (Line 59).

In an async function*, yield does not implicitly await a Promise. Line 59 emits a Promise<TYieldOut> instead of the resolved TYieldOut value. The tracked branch at Line 56 correctly uses await; the non-tracked branch should do the same.

🐛 Proposed fix
-          yield opts.yield.parseAsync(next.value);
+          yield await opts.yield.parseAsync(next.value);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tests/server/zAsyncIterable.ts` around lines 53 - 60, The
non-tracked branch in the async generator is yielding a Promise because it calls
opts.yield.parseAsync(next.value) without awaiting it; change the non-tracked
yield in the loop to await the parse result so it yields the resolved TYieldOut
(i.e., use await opts.yield.parseAsync(next.value)); this mirrors the tracked
branch which awaits opts.yield.parseAsync(data) and ensures the generator yields
resolved values instead of Promises (look for iterator, next, opts.tracked,
trackedEnvelopeSchema, tracked, and opts.yield.parseAsync in the loop).

68-79: ⚠️ Potential issue | 🟠 Major

Cast type parameters are semantically inverted relative to the corrected TYieldIn/TYieldOut meaning.

After this PR's fix, the naming convention is unambiguous:

  • TYieldIn = raw input type fed into opts.yield.parseAsync (what the raw iterable yields)
  • TYieldOut = validated output type produced by opts.yield.parseAsync

However, the cast at Line 68 still has them in the opposite positions for the outer z.ZodType<Output, Input>:

Position Value in cast Expected
Output (first) AsyncIterable<…TYieldIn…> should be AsyncIterable<…TYieldOut…> (validated)
Input (second) AsyncIterable<…TYieldOut…> should be AsyncIterable<…TYieldIn…> (raw)

The same inversion applies to TReturnIn/TReturnOut. This cast was unchanged in this PR but is now inconsistent with the corrected semantics. It likely needs to be updated to match:

🐛 Proposed fix for the cast
   ) as z.ZodType<
   AsyncIterable<
-    Tracked extends true ? TrackedEnvelope<TYieldIn> : TYieldIn,
-    TReturnIn,
+    Tracked extends true ? TrackedEnvelope<TYieldOut> : TYieldOut,
+    TReturnOut,
     unknown
   >,
   AsyncIterable<
-    Tracked extends true ? TrackedEnvelope<TYieldOut> : TYieldOut,
-    TReturnOut,
+    Tracked extends true ? TrackedEnvelope<TYieldIn> : TYieldIn,
+    TReturnIn,
     unknown
   >
 >;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tests/server/zAsyncIterable.ts` around lines 68 - 79, The cast on
the z.ZodType at the end of zAsyncIterable.ts is inverted: currently it declares
the ZodType output as AsyncIterable<...TYieldIn...> and input as
AsyncIterable<...TYieldOut...>; update that cast so the output (first generic)
is AsyncIterable of the validated types (TrackedEnvelope<TYieldOut> or
TYieldOut) and the input (second generic) is AsyncIterable of the raw types
(TrackedEnvelope<TYieldIn> or TYieldIn), doing the same swap for
TReturnIn/TReturnOut; locate the cast expression (the "as z.ZodType<...>" on the
AsyncIterable type returned by the function that uses opts.yield.parseAsync) and
swap TYieldIn<->TYieldOut and TReturnIn<->TReturnOut positions accordingly.
🧹 Nitpick comments (1)
packages/tests/server/zAsyncIterable.ts (1)

3-3: Use a namespace import for zod.

import { z } from 'zod' should be import * as z from 'zod'.

♻️ Proposed fix
-import { z } from 'zod';
+import * as z from 'zod';

As per coding guidelines: "Prefer namespace imports for validation libraries and large modules (e.g., import * as z from 'zod', import * as React from 'react')".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tests/server/zAsyncIterable.ts` at line 3, Replace the named import
of zod with a namespace import: change the import statement that currently reads
import { z } from 'zod' to use import * as z from 'zod' so all usages of the z
symbol in zAsyncIterable.ts continue to work while following the project's
import convention for validation libraries.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@packages/tests/server/zAsyncIterable.ts`:
- Around line 53-60: The non-tracked branch in the async generator is yielding a
Promise because it calls opts.yield.parseAsync(next.value) without awaiting it;
change the non-tracked yield in the loop to await the parse result so it yields
the resolved TYieldOut (i.e., use await opts.yield.parseAsync(next.value)); this
mirrors the tracked branch which awaits opts.yield.parseAsync(data) and ensures
the generator yields resolved values instead of Promises (look for iterator,
next, opts.tracked, trackedEnvelopeSchema, tracked, and opts.yield.parseAsync in
the loop).
- Around line 68-79: The cast on the z.ZodType at the end of zAsyncIterable.ts
is inverted: currently it declares the ZodType output as
AsyncIterable<...TYieldIn...> and input as AsyncIterable<...TYieldOut...>;
update that cast so the output (first generic) is AsyncIterable of the validated
types (TrackedEnvelope<TYieldOut> or TYieldOut) and the input (second generic)
is AsyncIterable of the raw types (TrackedEnvelope<TYieldIn> or TYieldIn), doing
the same swap for TReturnIn/TReturnOut; locate the cast expression (the "as
z.ZodType<...>" on the AsyncIterable type returned by the function that uses
opts.yield.parseAsync) and swap TYieldIn<->TYieldOut and TReturnIn<->TReturnOut
positions accordingly.

---

Nitpick comments:
In `@packages/tests/server/zAsyncIterable.ts`:
- Line 3: Replace the named import of zod with a namespace import: change the
import statement that currently reads import { z } from 'zod' to use import * as
z from 'zod' so all usages of the z symbol in zAsyncIterable.ts continue to work
while following the project's import convention for validation libraries.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9c62194 and 948fa29.

📒 Files selected for processing (1)
  • packages/tests/server/zAsyncIterable.ts

@aviu16

aviu16 commented Feb 28, 2026

Copy link
Copy Markdown
Contributor Author

Quick status update: branch is up to date and all checks are green on my side. If this looks good, would appreciate a maintainer review when convenient.

@Nick-Lucas Nick-Lucas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

@Nick-Lucas
Nick-Lucas merged commit 41723ce into trpc:main Feb 28, 2026
43 checks passed
@github-actions

github-actions Bot commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 1, 2026

This branch was successfully deployed

5 active deployments
Preview – www — 8770e21a Deployed Feb 28, 2026 by vercel[bot]
Preview – examples-todomvc — 8770e21a Deployed Feb 28, 2026 by vercel[bot]
Preview – next-prisma-starter — 8770e21a Deployed Feb 28, 2026 by vercel[bot]
Preview – og-image — 8770e21a Deployed Feb 28, 2026 by vercel[bot]
Preview – trpc-next-app-dir — 8770e21a Deployed Feb 28, 2026 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docs: TRPC Subscription zAsyncIterable refactoring after zod v4 migration

3 participants