Repository navigation
Support zod codecs (https://github.com/trpc/trpc/issues/6978) - #1
Open
lukeramsden wants to merge 3 commits into
Conversation
This change enables proper type inference for output functions where the input type differs from the output type, such as Zod codecs that use encode/decode transformations. Changes: - Add TInput type parameter to ParseFn (defaults to unknown for backward compat) - Update ParserCustomValidatorEsque to support explicit input types - Update inferParser to distinguish between unknown input (validators) and typed input (transforms/codecs) - Update createOutputMiddleware to accept typed ParseFn When using a typed output function like: const encoder = (value: bigint): string => value.toString(); procedure.output(encoder).query(() => 123n); The resolver is now correctly typed to return bigint (encoder input), and the client receives string (encoder output). This enables type-safe usage of Zod v4.1 codecs without type coercions.
Add a skipped test that documents how tRPC would work with actual Zod codecs once the project upgrades to Zod v4.1+. The test includes commented code showing the intended usage pattern with z.codec(). Codecs were introduced in Zod 4.1.0: https://zod.dev/codecs
Add native support for Zod v4.1+ codecs with a new `.outputEncoder()` method
on the procedure builder. This method uses the schema's encode() function
instead of parse(), properly handling the reversed type flow for output encoding.
Changes:
- Add ParserWithEncode type for schemas with encode capability
- Add inferEncoder type that swaps in/out for encoding direction
- Add getEncodeFn() to extract encode function from codec schemas
- Add hasEncodeFn() runtime check for codec detection
- Add outputEncoder() method to ProcedureBuilder
Usage with Zod v4.1+ codecs:
```typescript
const stringToBigInt = z.codec(z.string(), z.bigint(), {
decode: (s) => BigInt(s),
encode: (n) => n.toString(),
});
procedure
.outputEncoder(z.object({ value: stringToBigInt }))
.query(() => ({ value: 123n })); // Returns bigint, client receives string
```
The key difference from .output():
- output() uses parse(): _input (wire) -> _output (runtime)
- outputEncoder() uses encode(): _output (runtime) -> _input (wire)
1 task done
lukeramsden
commented
Dec 9, 2025
| middlewares: [createOutputMiddleware(parser)], | ||
| }); | ||
| }, | ||
| outputEncoder(encoder: Encoder) { |
Owner
Author
There was a problem hiding this comment.
I don't know if there is a better way to do this or not - I don't think codecs sit within Standard Schema correctly?
Owner
Author
There was a problem hiding this comment.
standard-schema/standard-schema#14
Seems like Zod has gone far beyond Standard Schema already
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.