panzacoder
panzacoder2w ago

Nested convex folders & Type inference

So I've been trying to organize my projects better and really liked that we could nest folders in convex, allowing me to break up schemas/queries/mutations, etc by entity or concern, rather than just a flat folder. This works well in general, allowing something like api.users.getUser to become api.users.queries.getUser. BUT, there is a huge drawback to this approach: Typescript inference becomes entirely generic for me when folders are nested, meaning I lose all type safety in my frontend queries. For instance, I have a file convex/auth.ts and also convex/users/functions.ts. These both work in my frontend, but when introspecting the type of the api object, we get very different results (see screenshot 1) Similarly the result of a function call from the auth.ts file is type-safe (screenshot 2) While the result of a function call from the users/functions.ts file is always any (screenshot 3) Is this just a limitation of the type inferences that are set up, or is there something more I can do to better enable type inference here?
No description
No description
No description
1 Reply
ian
ian2w ago
That's not expected. My suspicion is that there's some issue with circular types or something where the TypeScript server is giving up. Are you by chance putting the schema in one of these files that also exports a function? The query/mutation/action functions depend on schema.ts, which depends on the files it needs to import from. I find having a "pure" file (doesn't import anything with imports from _generated) like assistants/tables.ts that things import from. To debug, I would consider: - Make a simple project with this hierarchy with very basic functions to ensure it works in the "simple" case. Maybe it's something to do with your project's tsconfig, or your imports. - Look at how the types are defined in _generated/auth.d.ts and re-create that only importing files in the sub-folder to inspect what the types look like. - Gradually delete functions to see if that resolves it. Note: when you're adding/removing files, you need to make sure npx convex dev is running so the codegen files stay up to date Once you have something that is working, you can bisect to find what about your project is breaking it. With a minimal reproduction of the issue, I'd love to know what other users / Convex can do to fix this if they run into it

Did you find this page helpful?