No overload matches this call when outputting unions

Hello there πŸ‘‹, I have this simple procedure (we're not fully migrated on v10 yet, using interop):
export const databaseValidateUrl = trpc
.router<Context>()
.query('database.validateUrl', {
input: z.object({ url: z.string() }),
resolve: ({ input: { url } }) => {
const result = new ConnectionString(url)

if (result.validationErrors) {
return {
success: false,
validationErrors: result.validationErrors,
}
} else {
return { success: true }
}
},
})
export const databaseValidateUrl = trpc
.router<Context>()
.query('database.validateUrl', {
input: z.object({ url: z.string() }),
resolve: ({ input: { url } }) => {
const result = new ConnectionString(url)

if (result.validationErrors) {
return {
success: false,
validationErrors: result.validationErrors,
}
} else {
return { success: true }
}
},
})
Β  TypeScript is complaining:
J
jgouxβ€’389d ago
If I understand correctly, tRPC (or TS) can't figure out that I'm returning a potential union? { success: false, validationErrors: ValidationError } | { success: true }
J
jgouxβ€’389d ago
J
jgouxβ€’389d ago
full error message, it seems that the union is detected but conflicts with InferLast πŸ€”
N
Nickβ€’388d ago
Could you share the error as a code block? That file won’t display on mobile
J
jgouxβ€’388d ago
No overload matches this call.
Overload 1 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithInputOutputParser<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { url: string; }, { ...; }, { ...; }, unknown>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, { url: string; }, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors: ValidationErrorType; } | { success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }> | Promise<InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }> | Promise<InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type '{ success: boolean; validationErrors: ValidationErrorType; }'.
Types of property 'validationErrors' are incompatible.
Type 'undefined' is not assignable to type 'ValidationErrorType'.
No overload matches this call.
Overload 1 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithInputOutputParser<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { url: string; }, { ...; }, { ...; }, unknown>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, { url: string; }, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors: ValidationErrorType; } | { success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }> | Promise<InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }> | Promise<InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type 'InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>'.
Type '{ success: boolean; validationErrors?: undefined; }' is not assignable to type '{ success: boolean; validationErrors: ValidationErrorType; }'.
Types of property 'validationErrors' are incompatible.
Type 'undefined' is not assignable to type 'ValidationErrorType'.
Overload 2 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithInput<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { url: string; }, { ...; }>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, { url: string; }, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Overload 2 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithInput<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { url: string; }, { ...; }>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, { url: string; }, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Overload 3 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithoutInput<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { success: boolean; validationErrors: ValidationErrorType; }, unknown>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, undefined, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Types of parameters '__0' and 'opts' are incompatible.
Type '{ ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: undefined; type: ProcedureType; }' is not assignable to type '{ ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }'.
Types of property 'input' are incompatible.
Type 'undefined' is not assignable to type '{ url: string; }'
Overload 3 of 3, '(path: "database.validateUrl", procedure: CreateProcedureWithoutInput<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, {}, { success: boolean; validationErrors: ValidationErrorType; }, unknown>): Router<...>', gave the following error.
Type '({ input: { url } }: { ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }) => { ...; } | { ...; }' is not assignable to type 'ProcedureResolver<{ user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }, undefined, InferLast<{ success: boolean; validationErrors: ValidationErrorType; }>>'.
Types of parameters '__0' and 'opts' are incompatible.
Type '{ ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: undefined; type: ProcedureType; }' is not assignable to type '{ ctx: { user: { id: string; email: string; sub: string; entry?: string | undefined; role: "SUPERUSER" | "ADMIN" | "USER"; }; userId: string; }; input: { url: string; }; type: ProcedureType; }'.
Types of property 'input' are incompatible.
Type 'undefined' is not assignable to type '{ url: string; }'
It was to long for a single code block πŸ˜…
N
Nickβ€’388d ago
Do you have strict:true in your tsconfig? You could be right it's related to interop, you should migrate this to v10 and see how the errors change, as interop will be going away in v11 anyway and that's not far out
J
jgouxβ€’387d ago
that's the plan! yeah I'm on strict πŸ™‚ I cast the type for now.
More Posts
[help] Uncaught (in promise) TRPCClientError: Property description must be an object: uAfter building on linux, I visit the site. And Chrome console shows this error. But if I build on myHow can I make tRPC+NextJs APIs faster? (db and functions region is already same)Hi, I have migrated my website backend (https://clubofcoders.com) from NestJs + Prisma + Cloud Run tThrowing fastify errors when using fastify adapterHello, I'm using `fastifyTRPCPlugin` from `@trpc/server/adapters/fastify` and trying to throw errorsConfused about createProxySSGHelpersIf you can use this helper inside of `getServerSideProps` without having `ssr: true` what is the difHow to check if data is being prefetched?What is a method to check if ssg prefetching actually occurs? Through the network tab?How can I access ctx from inside of a procedure?``` const appRouter = t.router({ helloTab: t.procedure.input(z.object({ url: z.string().url() })).Async User Call in `createContext` (context.ts) or in `isAuthed` (in trpc.ts)Hi all! Should I be getting the user in all requests via createContext in the following async call `'req' of undefined in onError of express middlewareI've recently noticed that I get a bunch of errors regarding req object missing in ctx for onError pMutations and Queries in React Contexts causing unexpected behaviorsIn my organization, we recently moved to put all of our mutations/queries into React Contexts and ouCustom error managementHey peeps! I could've sworn I created a GitHub issue about this, but I must've been dreaming, becauscontext questionWhy are the context examples only showing opinionated examples with next/react auth ? Also i find itasync middlewareis it possible to define an async middleware? I want to do something like this but it throws errors The type of the second route of the nested route is neverhttps://github.com/StringKe/nx-mulit-trpc-type-error/blob/main/apps/app1/src/pages/index.tsx#L12errorFormatter ignored when using appRouter.createCallerMy errorFormatter works correctly in the actual application using an adapter, e.g. ``` trpcExpress.Is there a way to define the Websocket protocol when using wsLink()I am attempting to use tRPC with Azure's Pub/Sub Websockets service it appears to require custom proThe inferred type of this node exceeds the maximum length the compiler will serialize.Hey, there I am running into this error when I have more than 12 routers in the mergeRouters functioQuickstart not workingHello there, There is a type error in the quickstart example if i'm not mistaken. I pretty much coUse TRPC function inside of a TRPC functionHi all! We have a trpc query called getProduct. I want to use this getProduct query inside of anotheSupabase with trpcWhen trying to configure trpc with supabase, getting this kind of error in the console `tRPC failed Using tRPC for uploading audio filesI want to create an api router in tRPC but am not sure if the following code is doable with tRPC. If