Web Dev Cody
Web Dev Cody13mo ago

Running into this same typescript error

I know i've asked for help on this before, but I can't figure out how to resolve this issue... for some reason because the runQuery returns a Doc<"users">, this action just won't compile
No description
10 Replies
Web Dev Cody
Web Dev CodyOP13mo ago
this issue is pretty painful tbh this is my current fix
export const authAction = customAction(
action,
customCtx(async (ctx) => {
const userId = (await ctx.auth.getUserIdentity())?.subject;

if (!userId) {
throw new ConvexError("must be logged in");
}

const user: any = await ctx.runQuery(internal.users.getUserById, {
userId,
});

if (!user) {
throw new ConvexError("user not found");
}

const _id: Id<"users"> = user._id;
const isPremium: boolean = user.isPremium;

return {
user: {
_id,
userId,
isPremium,
},
};
})
);
export const authAction = customAction(
action,
customCtx(async (ctx) => {
const userId = (await ctx.auth.getUserIdentity())?.subject;

if (!userId) {
throw new ConvexError("must be logged in");
}

const user: any = await ctx.runQuery(internal.users.getUserById, {
userId,
});

if (!user) {
throw new ConvexError("user not found");
}

const _id: Id<"users"> = user._id;
const isPremium: boolean = user.isPremium;

return {
user: {
_id,
userId,
isPremium,
},
};
})
);
adding :any and then manually casting fields from that user: any
oscklm
oscklm13mo ago
Can you try and just return user directly like. return User; What is likely happening here is that you end up in an infinite loop. And since ur only returning user In this case, I would just return it directly. I’ve had a similar issue before, that might be related to this too. So you can read this thread, it’s valuable info for similar issues in the future: https://discord.com/channels/1019350475847499849/1199697643421573170
ballingt
ballingt13mo ago
This is a reasonable fix, if you can share a type between these files that you explicitly annotate the getUserById return type with and use here instead of any that would help. hmm looking closer I see this is about customAction, customCtx etc. It might help to type import getUserById into this file directly and annotate const user: ReturnType<typeof getUserById> directly. It might help to explicitly annotate the return type of getUserById in the implementation. We should write up a problem solving flowchart here, agree @Web Dev Cody that this is painful. A longer term fix is adding output validators to functions which we don't have official syntax for yet but is a direction we'll go down for better code generation. (OpenAPI spec generation, etc.)
Web Dev Cody
Web Dev CodyOP13mo ago
No description
Web Dev Cody
Web Dev CodyOP13mo ago
yeah same issue with the user: annotation the fact that the custom action callback returns the user object seems to just break it
Michal Srb
Michal Srb13mo ago
This cannot work, this cycle is still there:
No description
Michal Srb
Michal Srb13mo ago
@Web Dev Cody you want:
import { Doc } from "./_generated/dataModel";

/// ....

const user: Doc<"users"> | null = ....
import { Doc } from "./_generated/dataModel";

/// ....

const user: Doc<"users"> | null = ....
or similar (depends on what getUserById actually returns).
Web Dev Cody
Web Dev CodyOP13mo ago
I think I’ve also tried that I’ll try again in a bit
Ragpud
Ragpud12mo ago
Hey @Web Dev Cody , how did you get on with this problem? I'm using Michael's suggestion as a fix but just wondering if you've found a better way
Michal Srb
Michal Srb12mo ago
This is the correct workaround, and we have it now documented in the docs: https://docs.convex.dev/functions/actions#dealing-with-circular-type-inference
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment

Did you find this page helpful?