tRPCttRPC
Powered by
PieterP
tRPC•3y ago•
2 replies
Pieter

How to infer types from input?

When I call my procedure from the client I send an array of strings as an input. How can I infer the strings so they are returned in the response (see client code comment)

On server:
example: publicProcedure
    .input(z.object({ names: z.array(z.string()) }))
    .query(({ input }) => {
      return input.names.reduce((acc, name) => {
        return {
          ...acc,
          [name]: Math.random(),
        };
      }, {});
    }),
example: publicProcedure
    .input(z.object({ names: z.array(z.string()) }))
    .query(({ input }) => {
      return input.names.reduce((acc, name) => {
        return {
          ...acc,
          [name]: Math.random(),
        };
      }, {});
    }),


On client:
  const { data } = trpc.router.example.useQuery({
    names: ["Hello", "World"]
  })

  // infer types from query input / autocomplete
  const { Hello, World } = data
  const { data } = trpc.router.example.useQuery({
    names: ["Hello", "World"]
  })

  // infer types from query input / autocomplete
  const { Hello, World } = data
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

How to infer query types on client?
MadMaxMMadMax / ❓-help
3y ago
How to infer types of a query
HussamHHussam / ❓-help
3y ago
V10. How to infer types on V10?
lukasLlukas / ❓-help
4y ago
client unable to infer output types
advancedAadvanced / ❓-help
2y ago