Handling type inference and decoding in tRPC procedures can indeed be tricky, especially when dea...

Does anyone have any ideas on how I can avoid forcing the types in my tRPC procedure?

The reason I am doing this is cos a trpc procedure.input infers the client type based on the type returned from the callback. In this case, it is the decoded version of InputSchema. This doesn't work due to the decoded values such as: Utc , Branded types, etc.

What I have done is forced the Encoded version in the input - This way the client is typed correctly. Inside the mutation callback, I simply force the type back to the decoded value.

This works, however, I am wondering if there is a nicer way to do this 🤔 Perhaps some function in schema that is well suited to this.

const InputSchema = S.Struct({
  clientDocUpdatedAt: S.DateTimeUtcFromDate,
  text: S.String
});

export const UpdateDocTextMutation = protectedProcedure
  .input((input) => {
    return S.decodeUnknownSync(InputSchema)(input) as unknown as typeof InputSchema.Encoded;
  })
  .mutation(({ ctx, input: inputX }) => {
    const input = inputX as unknown as typeof InputSchema.Type;
    // input is decoded correctly :) 
e
Was this page helpful?