Teos
Teos•12mo ago

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!
ImgBB
1 hosted at ImgBB
Image 1 hosted in ImgBB
ImgBB
2 hosted at ImgBB
Image 2 hosted in ImgBB
ImgBB
3 hosted at ImgBB
Image 3 hosted in ImgBB
ImgBB
4 hosted at ImgBB
Image 4 hosted in ImgBB
ImgBB
5 hosted at ImgBB
Image 5 hosted in ImgBB
15 Replies
Teos
TeosOP•12mo ago
Anyone:/ 😅
jamwt
jamwt•12mo ago
@ian is our convex-helpers expert. he may have some input. he's certainly fought TS types a lot while making convex-helpers
ian
ian•12mo ago
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?
ian
ian•12mo ago
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:
export type User = Infer<typeof Users.doc>
export type User = Infer<typeof Users.doc>
No description
No description
ian
ian•12mo ago
My example is in the convex-helpers repo that you can play with locally
Teos
TeosOP•12mo ago
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).
Convex helper
Teos
TeosOP•12mo ago
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.
No description
No description
Teos
TeosOP•12mo ago
If I change my . schema not to use the fields but the TableX.table then all types are inferred as any .
No description
No description
No description
Teos
TeosOP•12mo ago
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 :/
Teos
TeosOP•12mo ago
Forgot to post how the type looks when defining it via the Table helper.
User Model type after defined via the Table helper
I believe this looks correct, i types are properly inferred - thats hovering over the Table
The Table type hover on schema.ts (seems the types are union here rather then object)
The final user shape in schema
Teos
TeosOP•12mo ago
Tested the same scenario with latest TS version, 5.3, 4,5 and 4,8 - no difference
ian
ian•12mo ago
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 imminently
Teos
TeosOP•12mo ago
First 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.
ian
ian•12mo ago
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 resolution
Teos
TeosOP•12mo ago
Wow, thanks a lot for that was a nasty one to find !

Did you find this page helpful?