Repository navigation
docs: fix zod v4 zAsyncIterable generic order in subscriptions guide - #7174
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
WalkthroughSwapped the ZodType generic parameter ordering for the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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.
@trpc/client
@trpc/next
@trpc/react-query
@trpc/server
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
fdec708 to
9c62194
Compare
|
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
left a comment
There was a problem hiding this comment.
There is a test file where this is copied from, can you update both?
|
@KATT Done. I updated the mirrored test helper file as requested ( |
There was a problem hiding this comment.
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 | 🔴 CriticalPre-existing bug: missing
awaitonyieldin the non-tracked path (Line 59).In an
async function*,yielddoes not implicitlyawaita Promise. Line 59 emits aPromise<TYieldOut>instead of the resolvedTYieldOutvalue. The tracked branch at Line 56 correctly usesawait; 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 | 🟠 MajorCast type parameters are semantically inverted relative to the corrected
TYieldIn/TYieldOutmeaning.After this PR's fix, the naming convention is unambiguous:
TYieldIn= raw input type fed intoopts.yield.parseAsync(what the raw iterable yields)TYieldOut= validated output type produced byopts.yield.parseAsyncHowever, 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 forzod.
import { z } from 'zod'should beimport * 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.
948fa29 to
8770e21
Compare
|
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. |
|
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. |
Summary
Fix the
zod v4output-validation snippet in the subscriptions docs to use the correctZodType<Output, Input>generic order.What changed
/www/docs/server/subscriptions.md(zod v4 section):yieldchanged fromz.ZodType<TYieldIn, TYieldOut>toz.ZodType<TYieldOut, TYieldIn>returnchanged fromz.ZodType<TReturnIn, TReturnOut>toz.ZodType<TReturnOut, TReturnIn>Why
ZodTypein v4 is parameterized as<Output, Input>. The previous order inverted input/output in the example and was confusing when adapting the helper.Closes #6952