convex-helpers `Table` helper loses type safety
Hello, Convex community!
First off, I want to express my admiration for Convex. I'm genuinely impressed by the onboarding process and the developer experience. It's been a delight working with the stack!
However, I've run into a puzzling issue while exploring Convex, specifically related to type inference in TypeScript, and I'm hoping someone might help shed some light on it.
In my project, I have a users.ts file that defines a User table using Convex's helper functions, like so:
https://ibb.co/BwVsx0G
in my schema.ts I use it as follows :
https://ibb.co/N7QnHyB
Here's the issue, on the user.ts file, if I hover-over the User type TS infers it as object of [x: string]: any
https://ibb.co/fdx5GhK
Hovering over the schema.users the type is like the below :
https://ibb.co/8mQ9tLF
Strangely, if I skip the Table() convex-helper and i export the userFields and directly use them in the schema.ts file with defineTable(userFields) <-- the types are inferred correctly (see below):
https://ibb.co/xJLMGBS
What am I doing wrong here ?
Any insights or guidance would be greatly appreciated.
Thanks in advance for your help!
15 Replies
Anyone:/ 😅
@ian is our convex-helpers expert. he may have some input. he's certainly fought TS types a lot while making convex-helpers
One question is whether you have an import cycle - is the user table definition in the same file as the schema? Or its own file?
I am trying to reproduce but it's working for me, even when I have it in a another file where I'm referencing the schema.
I wonder what this looks like for you:


My example is in the convex-helpers repo that you can play with locally
hey, thanks for the reply.
The user.ts is on its own file, I also tried with the convex-helpers example and hovering I see its infered as any to attached (convex helper screenshot).
Also in my project i have the following (cleaned up a bit to have a simpler repro).

In the screenshots here, I am still using userFields in schema.ts The Infer loses type safety but type User is correctly showing the shape.


If I change my . schema not to use the fields but the TableX.table then all types are inferred as any .



I mean, its not end of the world, I can export the fields and use them with defineTable, but it's just puzzling not understanding why it happens xexe :/
Forgot to post how the type looks when defining it via the Table helper.




Tested the same scenario with latest TS version, 5.3, 4,5 and 4,8 - no difference
Do you have a repro you can share with me as a repo so I can fiddle around? I wonder if it's a tsconfig thing, or some way it's being imported or interpreted. Those errors make me think the convex-helpers types aren't being imported correctly
Sorry you're running into this! I'm actually working on a proposal internally that will make the
Table
helper not as necessary - you'd be able to access the fields more easily on the official schema, but that isn't shipping imminentlyFirst off, no need to apologize at all! Honestly, everything has been running smoothly so far, and these hiccups are just part of the process. I really appreciate your dedication and the hard work you're putting into improving the user experience.
I've gone ahead and invited you as a collaborator to my repository on github. Please feel free to use the
repro
branch to explore and tinker as much as you need.I pushed a fix to the
repro
branch @Teos - I couldn't find a way around adding the tsconfig
back to the convex/
directory. I added a paths
config there to match your root one, so @convex/...
should work.
The core issue was module resolution not being "node" which is what currently works. Hoping to soon improve the bundling which may remove the need for node module resolutionWow, thanks a lot for that was a nasty one to find !