Skip to content

Input schemas defined with Zod does not use the inferred input type #1182

Closed

Description

@kimroen

Summary

trpc.router()
  .query('greeting', {
    input: z.object({
      hi: z.string().transform(s => s.length),
    }),
    resolve({ input }) {
      input.hi // => `number`, as expected
    },
  });

// Somewhere else

client.query('greeting', { hi: 'my greeting' }); // => ERROR: expected `number`

CodeSandbox

When defining an input schema using zod, I would expect the call site to accept the z.input version of the schema.

Details

When you define a schema with zod, like this simple one:

const GreetingSchema = z.object({
  hi: z.string(),
});

You can infer its type with z.infer like this:

type Greeting = z.infer<typeof GreetingSchema>;

// Equals

type Greeting = {
  hi: string;
}

However, in zod you can also add transformers that change the type of a field, which means the input and output types of the schema are different.

Here's an example of that:

const GreetingSchema = z.object({
  hi: z.string().transform(s => s.length),
});

z.infer infers the output type of this:

type InferredGreeting = z.infer<typeof GreetingSchema>;

// Equals

type InferredGreeting {
  hi: number;
}

To get at the input type, zod also has a z.input helper (and z.output, which z.infer is an alias of), meaning you can get at the version we want:

type InputGreeting = z.input<typeof GreetingSchema>;
type OutputGreeting = z.output<typeof GreetingSchema>;

// Equals

type InputGreeting {
  hi: string;
}

type OutputGreeting {
  hi: number;
}

This is also mentioned in the README of Zod.

This concept applied to tRPC:

trpc.router()
  .query('greeting', {
    input: z.object({
      hi: z.string().transform(s => s.length),
    }),
    resolve({ input }) {
      input.hi // => `number`, as expected
    },
  });

// Somewhere else

client.query('greeting', { hi: 'my greeting' }); // => ERROR: expected `number`

In other words, for tRPC I would expect the type required to call a function to be the z.input type, and the type available in the resolve function to be the z.output type.

Activity

  1. kimroen commented on Oct 28, 2021

    @kimroen
    Author

    It originally looked like I wasn't able to reproduce this in the sandbox (the argument to client.query was string and the parameter in resolve was number), but then the type check failed and the error showed up in the editor when running the project. Not sure what's up with that!

  2. changed the title [-]Input schemas defined with Zod does not use the input type[/-] [+]Input schemas defined with Zod does not use the inferred input type[/+] on Oct 28, 2021
  3. added
    🧙🏻 typescript wizardry
    on Oct 29, 2021
  4. KATT commented on Oct 29, 2021

    @KATT
    Member

    Thanks for this @kimroen! It looks like we need something similar to the way we handle context swapping on our router!

    We'll definitely have a stab at this - @mmkal, do you feel inclined? This is right up your alley I feel 😊

  5. added a commit that references this issue on Oct 29, 2021
  6. added
    🙋‍♂️ help wanted
    and removed
    🙋‍♂️ help wanted
    on Oct 29, 2021
  7. KATT commented on Oct 29, 2021

    @KATT
    Member

    Got a working branch in #1189 - just waiting for feedback

  8. KATT commented on Oct 29, 2021

    @KATT
    Member
  9. allcontributors commented on Oct 29, 2021

    @allcontributors
    Contributor

    @KATT

    I've put up a pull request to add @kimroen! 🎉

  10. github-actions commented on Oct 4, 2022

    @github-actions
    Contributor

    This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.

  11. locked as resolved and limited conversation to collaborators on Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    🧙🏻 typescript wizardry

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions