sp85
sp853y ago

Coming from tRPC land I want to set up

Coming from tRPC land, I want to set up end-to-end typing with Convex. What is the suggested pattern for getting this done? For example, I have a function — projects:create — which has the following signature:
const create = mutationWithSession(async ({ db, auth, session }, project: Pick<Document<"projects">, "name", "description">): Promise<Document<"projects">> => { ... });
const create = mutationWithSession(async ({ db, auth, session }, project: Pick<Document<"projects">, "name", "description">): Promise<Document<"projects">> => { ... });
But, when I go to call useMutation("projects:"create") on the React side of things, the return type is inferred as Promise<any> whereas I'd like it to be Promise<Document<"projects">>. Thanks in advance!
19 Replies
nipunn
nipunn3y ago
This should work with convex! I believe you just need to keep npx convex dev running in the background to make sure the generated type annotations stay up to date.
nipunn
nipunn3y ago
Here's an example from my app: (screenshot from vscode hovering over the mutation)
No description
nipunn
nipunn3y ago
const m = useMutation("oncall:updateCurrentOncall");
const m = useMutation("oncall:updateCurrentOncall");
And I hovered over m
sp85
sp85OP3y ago
That also works for me for simple return types (number, string, etc.). But not when I try and return a document (from the "projects" collection in my case). Is this expected?
sshader
sshader3y ago
That's unexpected! Sometimes I've found that I need to open the _generated/api.d.ts file in my editor (I use VSCode) to get it to pick up on the latest changes. I'm also curious if running TypeScript over your project directly catches type errors.
sp85
sp85OP3y ago
No luck with either of those unfortunately. I'm also using VSCode, using VSCode's TS version of 4.9.5, and am running convex in a NextJS app. I can try putting together a reproduction if there are no alternatives
ballingt
ballingt3y ago
@sp85 Do you have a schema.ts file?
ballingt
ballingt3y ago
Defining a Schema | Convex Developer Hub
End-to-end type safety requires typing your tables.
sp85
sp85OP3y ago
Yup!
nipunn
nipunn3y ago
What does mutationWithSession look like for you? I wonder if types are getting lost on the way via that middleware pattern. I am trying to repro currently and can't. I made a function that returns a document
No description
nipunn
nipunn3y ago
defined as such on the convex side:
No description
sp85
sp85OP3y ago
I got mutationWithSession from https://github.com/get-convex/convex-helpers/blob/main/convex/sessions.ts . Not sure if that's the cause though, testing with a regular mutation with a document return type yields the same result.
GitHub
convex-helpers/sessions.ts at main · get-convex/convex-helpers
A collection of useful code to complement the official packages. - convex-helpers/sessions.ts at main · get-convex/convex-helpers
ballingt
ballingt3y ago
If you can post the code would love to see it, or if you want to hop on a call we could look together
sp85
sp85OP3y ago
A call would be preferable, thanks
ballingt
ballingt3y ago
send you a DM with a link Wrapping this up: there's an interesting issue with TypeScript as configured in the default Next.js template and perhaps any use of the tsconfig.json baseUrl property. With a "baseUrl": ".", TypeScript interprets import * as s from "foo" first as an import of ./foo.ts, then only if that file doesn't exist as an import of the npm package foo. For convex this means convex/schema.ts and convex/schema package export being confused. Changing the name of the convex/ directory (which also needs to be updated in convex.json) works around this for now. sleuthed by @sshader
nipunn
nipunn3y ago
Oh wow. Great find.
ballingt
ballingt3y ago
and big props to @sp85 for figuring out that baseUrl made the difference, that's what got us close
ballingt
ballingt3y ago
GitHub
baseUrl in create-next-app causes local files to shadow npm package...
Verify canary release I verified that the issue exists in the latest Next.js canary release Provide environment information tomb@Thomass-MacBook-Pro-2 abcef % npx --no-install next info Operating S...
ballingt
ballingt3y ago
this may be a TypeScript issue, but going to read more about how baseUrl is supposed to work before claiming that

Did you find this page helpful?