SamS
Convex Community6mo ago
3 replies
Sam

Type Inference Issue with Custom Auth Wrappers

Hi! I'm having some issues with type inference with the custom wrappers. Don't know if its a project structure issue or a custom wrappers issue. When I use the convex r2 component I am getting full typesafety and autocomplete working in the frontend (nextjs) but when I write my own wrapped queries/mutations, I get back the function as any. I'm using convex-helpers to create custom auth wrappers, but the types aren't being inferred correctly in my Next.js frontend.

Example Simple Custom Wrapper

// In convex/core/auth/index.ts
export const withAuthQuery = customQuery(
query,
customCtx(async (ctx) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) throw new Error("Not authenticated");

const user = await ctx.db
.query("users")
.withIndex("by_user_id", q => q.eq("userId", identity.subject))
.unique();

if (!user) throw new Error("User not found");

return { user };
}),
);

Example Function Using the Wrapper

// In convex/features/users/public.ts
export const getProfile = withAuthQuery({
args: {},
handler: async (ctx) => {
// ctx.user is available from the wrapper
return ctx.user;
}
});

The Problem

In the Next.js frontend:
// In Next.js - types show as 'any'
import { api } from "@app/convex/_generated/api";
import { useQuery } from "convex/react";

// No type inference - shows as any
const profile = useQuery(api.features.users.public.getProfile);
// ^-- type of getProfile when I hover over it is any instead of the actual user type. Autocomplete works for everything up until the getProfile

The generated _generated/api.js contains:
export const api = anyApi;

Which doesn't preserve type information for custom functions. Question: Is there a way to get proper type inference for functions created with convex-helpers' custom wrappers? The types work fine within the Convex package, but are lost when imported in the frontend.
Was this page helpful?