T
tRPC

How to infer types from input?

How to infer types from input?

APA. Perkamentus3/7/2023
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
APA. Perkamentus3/7/2023
I think this is only for getting the response- or the input types. What I want for example:
const {.data } = trpc.example.useQuery(['one', 'two'])

// type of data variable
// readonly ["one", "two"] <- infered from input
const {.data } = trpc.example.useQuery(['one', 'two'])

// type of data variable
// readonly ["one", "two"] <- infered from input

Looking for more? Join the community!

T
tRPC

How to infer types from input?

Join Server