tRPCttRPC
Powered by
DangerZoneD
tRPC•2y ago•
1 reply
DangerZone

Validating inputs and outputs only via typescript

Hey everyone,
I am trying to create an appRouter with
ts-morph
ts-morph
but I am having trouble with passing the
zod
zod
schema/object instance to it (since I need to pass the original implementation, not the instance).
I was thinking about a way to work around this and was wondering if I could somehow just pass the zod typescript type in the inputs and outputs - keep in mind that my usecase only calls for the type definitions to work, not the actual implementation.

import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
  name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
  UserRouter: {
    hello: publicProcedure.input<RandomType>().query((opts) => {
      const name = opts.input.name;
      return {
        greeting: `Hello ${opts.input.name}`,
      };
    }),
  },
});
export type AppRouter = typeof appRouter;
import { initTRPC } from '@trpc/server';
import { z } from 'zod';

const t = initTRPC.create();
const publicProcedure = t.procedure;

const randomSchema = z.object({
  name: z.string(),
});

type RandomType = z.infer<typeof randomSchema>;

const appRouter = t.router({
  UserRouter: {
    hello: publicProcedure.input<RandomType>().query((opts) => {
      const name = opts.input.name;
      return {
        greeting: `Hello ${opts.input.name}`,
      };
    }),
  },
});
export type AppRouter = typeof appRouter;


here you can see I am trying to pass on;y the
RandomType
RandomType
, but ofc I don't really know what I am doing 😄
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?

Similar Threads

Accepting a DecoratedProcedure with inputs and outputs that extend some given types
lithdewLlithdew / ❓-help
3y ago
Validating Permissions
RhysRRhys / ❓-help
4y ago
useInfinieQuery with initialData, Refetch only after second change of inputs
.westsaid..westsaid / ❓-help
3y ago
Is there a typescript performance gain if I infer my router outputs once, and then export them?
NotZeldaNNotZelda / ❓-help
10mo ago