tRPCttRPC
Powered by
MugetsuM
tRPC•3y ago•
4 replies
Mugetsu

Discriminated union handle

How Do I handle Input type if its discriminated union??

export const batchTasksRouter = createTRPCRouter({
  requestExport: procedure
    .input(
      z.discriminatedUnion('type', [
        z.object({
          type: ExportsZSchema,
        }),
        z.object({
          type: ExportsZSchema,
          id: z.string().uuid(),
        }),
        z.object({
          type: ExportsZSchema,
          id: z.string().uuid(),
          query: z.object({
            from: z.string(),
            to: z.string(),
            type: z.string().optional(),
          }),
        }),
      ]),
    )
    .query(async ({ ctx, input }) => {
      const { id, query, type } = input // <------- TYPESCRIPT ERRRORS that id and query not exists
      let urlSuffix = type

      if (id) urlSuffix += `/${id}`

      if (query) urlSuffix += `?${querystring.stringify(query)}`
...
    }),
})
export const batchTasksRouter = createTRPCRouter({
  requestExport: procedure
    .input(
      z.discriminatedUnion('type', [
        z.object({
          type: ExportsZSchema,
        }),
        z.object({
          type: ExportsZSchema,
          id: z.string().uuid(),
        }),
        z.object({
          type: ExportsZSchema,
          id: z.string().uuid(),
          query: z.object({
            from: z.string(),
            to: z.string(),
            type: z.string().optional(),
          }),
        }),
      ]),
    )
    .query(async ({ ctx, input }) => {
      const { id, query, type } = input // <------- TYPESCRIPT ERRRORS that id and query not exists
      let urlSuffix = type

      if (id) urlSuffix += `/${id}`

      if (query) urlSuffix += `?${querystring.stringify(query)}`
...
    }),
})

TS2339: Property 'id' does not exist on type ...
TS2339: Property 'id' does not exist on type ...

TS2339: Property 'query' does not exist on type ...
TS2339: Property 'query' does not exist on type ...
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

Incorrect type inference with discriminated unions.
dechoDdecho / ❓-help
3y ago
Discriminated output based on discrimiated input
Lautaro_dapinLLautaro_dapin / ❓-help
3y ago
Handle Multipart form data.
Rasel HossainRRasel Hossain / ❓-help
2y ago