ed
ed2mo ago

Convex code generation where it doesn't give types when I run a mutation / query

I'm running into an issue with my Convex code generation where it doesn't give types when I run a mutation / query. Below would be an example of a query that I would send, but this would always be the type definition.
import { v } from "convex/values";

import type { Doc } from "./_generated/dataModel";
import { internalMutation, internalQuery } from "./_generated/server";

export const getUserSummary = internalQuery({
args: { userId: v.id("users") },
handler: async (ctx, args): Promise<Doc<"userMemorySummaries"> | null> => {
return ctx.db
.query("userMemorySummaries")
.withIndex("by_user", (q) => q.eq("userId", args.userId))
.first();
},
});
import { v } from "convex/values";

import type { Doc } from "./_generated/dataModel";
import { internalMutation, internalQuery } from "./_generated/server";

export const getUserSummary = internalQuery({
args: { userId: v.id("users") },
handler: async (ctx, args): Promise<Doc<"userMemorySummaries"> | null> => {
return ctx.db
.query("userMemorySummaries")
.withIndex("by_user", (q) => q.eq("userId", args.userId))
.first();
},
});
31 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ed
edOP2mo ago
No description
ed
edOP2mo ago
@Clever Tagline super sorry just got back to my desk
Clever Tagline
Clever Tagline2mo ago
No worries. Two things stick out to me: 1. Usually when importing Doc I'm not used to seeing only the type imported. It normally imports like import { Doc } from .... However this might be irrelevant when considering what's next. 2. I'm also not used to seeing the handler return type spelled out. I've never done that myself as the function builder takes care of that automatically. And it's not the handler that you'd need to explicitly type because that's used internally, but that's beside the point. The way I would expect to see this coded is like this:
import { v } from "convex/values";

import { internalMutation, internalQuery } from "./_generated/server";

export const getUserSummary = internalQuery({
args: { userId: v.id("users") },
handler: async (ctx, args) => {
return ctx.db
.query("userMemorySummaries")
.withIndex("by_user", (q) => q.eq("userId", args.userId))
.first();
},
});
import { v } from "convex/values";

import { internalMutation, internalQuery } from "./_generated/server";

export const getUserSummary = internalQuery({
args: { userId: v.id("users") },
handler: async (ctx, args) => {
return ctx.db
.query("userMemorySummaries")
.withIndex("by_user", (q) => q.eq("userId", args.userId))
.first();
},
});
If you build it like that and hover over getUserSummary, you'll see that it automatically figures out the return type, so that anything calling it—like await ctx.runQuery—will know what type is returned. Only recently did I see a way of explicitly indicating the return of the query, and it's done by adding a returns property to the object passed to internalQuery; e.g.
export const getUserSummary = internalQuery({
args: { /* args as usual */ },
returns: { /* Return details here */ },
handler: async (ctx, args) => {
/* Etc */
},
});
export const getUserSummary = internalQuery({
args: { /* args as usual */ },
returns: { /* Return details here */ },
handler: async (ctx, args) => {
/* Etc */
},
});
If you really want to go that route, the details are probably listed in the docs somewhere, but I haven't used that myself because I've never had the need. The built-in return type handling has always worked fine for me. I gotta go, but I'll check back on this tomorrow. If you still can't get it working and anyone else wants to jump in and help in the meantime, feel free.
ed
edOP2mo ago
Appreciate the write-up a lot, just remove the type from Doc and the spelled out return type. I'm noticing even after i write those changes that the types are still the exact same. the reason why i hard typed it out (which still doesn't work lol) is because the engine wasn't able to figure it out. this is super strange and possibly due to one small issue that i'm not aware of, maybe it's the v1 template i used?
GitHub
GitHub - get-convex/v1: An open-source starter kit based on Midday....
An open-source starter kit based on Midday. Ported to Convex. - get-convex/v1
No description
Clever Tagline
Clever Tagline2mo ago
maybe it's the v1 template i used?
Perhaps? I've not used v1 before, so I'm not familiar with anything in it that might conflict with Convex. That's definitely weird that changing those items didn't improve anything. As for the hard typing thing, I believe the issue with what you had before was that you were typing the return from the handler. However, if I've learned anything about Convex, it's that there are layers hidden underneath, and my guess is that the part returning the data is likely not the anonymous function assigned to the handler in that definition. That function is just passed to internalQuery, and something inside that is likely handling the actual return, so trying to force-type anything is moot because it's all under the covers (so to speak). I'm gonna tag @erquhart for help with this, as I'm out of ideas.
erquhart
erquhart2mo ago
Did the v1 template already do this for you before any changes, with it's existing queries? I can take a look and try and repro if so
ed
edOP2mo ago
i'm not sure because i only noticed the type differences when i created like my 5th function thank you for your help <3
erquhart
erquhart2mo ago
You have convex dev running and seeing green check success when you make changes?
ed
edOP2mo ago
✔ 16:57:01 Convex functions ready! (5.35s)
erquhart
erquhart2mo ago
Can you share your convex/_generated/api.d.ts
ed
edOP2mo ago
erquhart
erquhart2mo ago
What file is this function in getUserSummary
ed
edOP2mo ago
the function is defined in packages/backend/convex/memory.ts this is my tsconfig
{
/* This TypeScript project config describes the environment that
* Convex functions run in and is used to typecheck them.
* You can modify it, but some settings required to use Convex.
*/
"compilerOptions": {
/* These settings are not required by Convex and can be modified. */
"allowJs": true,
"strict": true,
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,

/* These compiler options are required by Convex */
"target": "ESNext",
"lib": ["ES2021", "dom", "DOM.Iterable"],
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"isolatedModules": true,
"noEmit": true
},
"include": ["./**/*"],
"exclude": ["./_generated"]
}
{
/* This TypeScript project config describes the environment that
* Convex functions run in and is used to typecheck them.
* You can modify it, but some settings required to use Convex.
*/
"compilerOptions": {
/* These settings are not required by Convex and can be modified. */
"allowJs": true,
"strict": true,
"moduleResolution": "Bundler",
"jsx": "react-jsx",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,

/* These compiler options are required by Convex */
"target": "ESNext",
"lib": ["ES2021", "dom", "DOM.Iterable"],
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"isolatedModules": true,
"noEmit": true
},
"include": ["./**/*"],
"exclude": ["./_generated"]
}
erquhart
erquhart2mo ago
I'm seeing the template does seem to have types Did you make changes to tsconfig
ed
edOP2mo ago
nope, just cloned the v1 template again and i see the types 🤔 copied over the tsconfig for convex backend again
ed
edOP2mo ago
i noticed that the v1 has types for getAuthSessionId
No description
ed
edOP2mo ago
while mine has nothing (even when they're imported from the same source)
No description
erquhart
erquhart2mo ago
That's pretty odd What am I looking at in those two screenshots, they look identical but one has types
ed
edOP2mo ago
yeah exactly one is from teh template one is not, is there somewhere i should look into to check my types? ive been regenerating them using npx convex dev but is there anywhere else i shoudl look there's definitely something wrong w/ the type system but i can't debug from where
erquhart
erquhart2mo ago
Are these two files siblings?
ed
edOP2mo ago
this is from v1 repo the second one is from my repo
erquhart
erquhart2mo ago
Any chance you have typechecking disabled in convex/schema.ts
ed
edOP2mo ago




erquhart
erquhart2mo ago
oh no typechecking is enabled through cli
ed
edOP2mo ago
i see types through schema, but nothing else
No description
erquhart
erquhart2mo ago
If you write new functions in the template's files, they're typed correctly? Like this only happens in files you add?
ed
edOP2mo ago
correct, the template repository works with types — but the one that i forked doesn't so it might be because of some config on my repository that i shouldn't have touched
erquhart
erquhart2mo ago
yeah I'd maybe get a fresh fork and port your added code over bit by bit until you find the culprit
ed
edOP2mo ago
😭 is there any where else i can check before doing that i think i found the issue it's the node modules... but i'm seeing a completely different issue now, which is two exact same schemas erroring each other out
ed
edOP2mo ago

Did you find this page helpful?