Repository navigation
Description
Describe the feature you'd like to request
With Zod V4.1 introducing Codecs, we get a more "lightweight" option for encoding and decoding data (e.g., instead of using Transformers). However, the current implementation of the type inference binds the returned type of a procedure to the output type of the parse function, rather than the input.
This restricts the output schema in that it can't apply transformations and is only capable of validations 1:1.
For example, if I have a schema with a codec stringToBigInt (as per the docs) and would look to do something like the code below, I'll face type inference issues stating that Type string is not assignable to bigint, even though the underlying schema and runtime types would align.
function encode<TSchema extends z.ZodType>(schema: TSchema) {
return (value: unknown) => schema.encode(value as z.output<TSchema>) // as-coercion needed due to the input type of the `ParseFn` being `unknown`
}
//...
procedure.output(encode(z.object({x: stringToBigInt}))).query(() => ({x: 0n}))Describe the solution you'd like to see
To get this working, it seems like what's needed is to enable chained middlewares to align output types of prior middlewares as input types to the successors.
The MiddlewareOKResult could have a type parameter which defaults to unknown which then is set in createOutputMiddleware instead of leaving the outputMiddleware as an AnyMiddlewareFunction, which is used to narrow the type on the middleware output type.
From what I can see, the ParseFn type would also need an argument type parameter TInput (default to unknown), e.g.,
type ParseFn<TOutput, TInput = unknown> = (value: TInput) => Promise<TOutput> | TOutputof which there are then some areas where types need to be updated to ensure it aligns well.
Describe alternate solutions
Right now, I have to do type coercions that I don't necessarily like to make this work. The solution is to do the following (using the initial example):
function encode<TSchema extends z.ZodType>(schema: TSchema) {
return (value: unknown) => schema.encode(value as z.output<TSchema>) // as-coercion needed due to the input type of the `ParseFn` being `unknown`
}
//...
const schema = z.object({x: stringToBigInt})
procedure.output(encode(schema)).query(() => ({x: 0n}) as unknown as z.input<typeof schema>) // coerce to input type although it's the output shape to be encoded through the codec to be the input shapeAdditional information
If I have missed any requirements and this is a useful feature for others, please let me know how I can assist.
If there are other ways of going about this in a more typesafe way, do let me know as well!
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Activity
KATT commentedon Oct 20, 2025
Not sure if I follow, but curious to see a PR :)
felixnorden commentedon Oct 20, 2025
Will see what I can do!
To try and simplify: Zod Codecs act as bidirectional transforms and using schemas that have codecs in them mean that the input type (
TInput) and the output type (TOutput) to the schema will differ and cause either type error or runtime errors.The returned value of a procedure is either of
TInputorTOutput; if it's ofTInput, the type check will fail as the value isn't matching the typeTOutput. If it's instead of typeTOutput, then there will be a runtime error as the schema will see that the value is not matchingTInputand the validation will fail.Sourav12061999 commentedon Nov 20, 2025
Hi @felixnorden , if you don't mind could you please leave it to me, would love to work on it
felixnorden commentedon Nov 20, 2025
Yeah feel free to if you want to pick it up, I've begun tinkering with it after work hours to help facilitate parts of our work, so if you want to bounce ideas, let me know @Sourav12061999
lukeramsden commentedon Dec 9, 2025
Most certainly not proposing to merge this code but here's some exploratory work I did in a Claude Code session - how does this line up with everyone's thoughts?
lukeramsden#1
bas0N commentedon Dec 13, 2025
@felixnorden If no one has started on this yet, I can take it
felixnorden commentedon Dec 16, 2025
@bas0N , I've held off as people above have wanted to take a stab at it, so feel free to do so yourself. Otherwise, I'll aim to get to this during the Holidays 🤝
bas0N commentedon Dec 23, 2025
@felixnorden pr is ready with the implementation you outlined
KATT commentedon Nov 2, 2026
🤖 Passing the schema itself (not a function wrapping it) already does this in v11: the resolver returns the output schema's input type, the client gets its output type, and the transform runs on the server. A plain
(value: unknown) => …parser has one type for both sides, which is whyencode()hit the error.Reusing one codec both ways (
.encode()on the output) isn't supported. As Nick said on #7073, a PR is welcome if userland tests show it's more typesafe than this. Docs: https://trpc.io/docs/server/validators#output-validators.