T
tRPC

How to get response type depending on the input params?

How to get response type depending on the input params?

APA. Perkamentus3/8/2023
Simple example of a normal JS function with generics:
const example = <T extends unknown>(params: T): T => {
return params
}

// TYPE: const params: readonly ["Hello", "World"]
const params = ['Hello', 'World']

// TYPE: const result: readonly ["Hello", "World"]
const result = example(params)

// TYPE: const str1: string, const str2: string
const [str1, str2] = result
const example = <T extends unknown>(params: T): T => {
return params
}

// TYPE: const params: readonly ["Hello", "World"]
const params = ['Hello', 'World']

// TYPE: const result: readonly ["Hello", "World"]
const result = example(params)

// TYPE: const str1: string, const str2: string
const [str1, str2] = result
This is what I want to achieve with TRPC. If I send some params I want my respone type be able to change depending on the params. How can I achieve that?
// TYPE: const params: readonly ["Hello", "World"]
const params = ['Hello', 'World']

const { data } = trpc.router.model.example(params)

// I want the following type returned based on the params
// TYPE: const result: readonly ["Hello", "World"]
const result = data;

// I want the same result as in the simple example
// TYPE: const str1: string, const str2: string
const [str1, str2] = result
// TYPE: const params: readonly ["Hello", "World"]
const params = ['Hello', 'World']

const { data } = trpc.router.model.example(params)

// I want the following type returned based on the params
// TYPE: const result: readonly ["Hello", "World"]
const result = data;

// I want the same result as in the simple example
// TYPE: const str1: string, const str2: string
const [str1, str2] = result
I already checked this page in the docs, but this does not describe my use case (https://trpc.io/docs/infer-types)
Nnlucas3/8/2023
Simplest way would be using a discriminated union: https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#discriminating-unions But that's not exactly what you're showing in the TS example Maybe a router factory if it's for a known set of entities and you don't mind splitting them up among routes: https://dev.to/nicklucas/trpc-patterns-router-factories-and-polymorphism-30b0 True covariance of inputs in TS is a challenge because it can be achieved via method overloads, but that's not type-safe on the inside

Looking for more? Join the community!

T
tRPC

How to get response type depending on the input params?

Join Server
Recommended Posts
how to build tRPC and Prisma with express?I am using TSC as the official docs example does. But when I use paths in tsconfig.json, It does notHow 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 theHow to infer types of a queryCoinsider the example `const hello = trpc.hello.useQuery();` I would like to export the type we get Data Visualisation/Chartsis anyone using tRPC on data visualisation (say bar chart/line chart) in a scenario close to a bankitrpc + AWS Lambda (through cdk)Hi all, has anyone successfully integrated tRPC with AWS Lambda? My current stack is API Gateway + LIs it possible to narrow an output schema if the query optionally doesn't return all fields?I have a router procedure that has an input schema that has an optional `filter` that changes the shFetching different server url than defined in configIs it possible to access the reactQuery instance and fetch different server url? I would like to useinput using z.or not working properlyi have an input like this let input = z.object({ name: z.string().optional() }).or(z.object({ How can I disable batching with fastify adapter?I cant seem to find a way to disable batching for my server, and this link doesnt help me much httpsIssue with monorepo architecture ant tRPCHi, we had an issue with batched requests that if we batch some requests they produce a TRPCClientErUsing tRPC in CRON jobsHey everyone, this might be a very stupid question, but is it possible to use tRPC inside a CRON joasync createContext for Express AdapterBeen debugging an odd behavior for the past hour, it seems like that an async function does not work