whoami
whoami2y ago

end to end typing

Does convex support generating types for input / output of the cloud function that can be directly plugged in react/typescript?
7 Replies
ian
ian2y ago
Yes the types from your functions are automatically available from the react client Alex wrote a post about how we do this I can share, but short answer yes
whoami
whoamiOP2y ago
Hey Ian, mind sharing the article? I didn't find any examples in the convex-demo repo and you may consider adding one, thanks
ian
ian2y ago
Here you go- hasn’t been publicly published but https://stack.convex.dev/end-to-end-ts
End-to-end TypeScript with Convex
Convex provides automatic type safety all the way from your database schema to your React app. How does it work? Hint: we use some wild TypeScript.
ian
ian2y ago
The basics though is that it works by default, are they not working for you already? You don’t have to set anything up
whoami
whoamiOP2y ago
How do I get the explicit type of the returns? In my case
const data = useQuery('tools:list', {
org: org.id,
paginationOpts: { numItems: 20 },
})

// How do I get the type of `data[0].page` like the one I defined below? I need to pass type Tool to a generic function
type Tool = {
_id: Id<'tools'>
name: string
category: string
type: string
config: any
}
const data = useQuery('tools:list', {
org: org.id,
paginationOpts: { numItems: 20 },
})

// How do I get the type of `data[0].page` like the one I defined below? I need to pass type Tool to a generic function
type Tool = {
_id: Id<'tools'>
name: string
category: string
type: string
config: any
}
ian
ian2y ago
If tools is a table, you can use Doc<“tools”>. There’s actually some other nifty things called out in some docs on typescript types. The return type is also pretty easy but I’m afk so I’ll have to follow up later. You can click into useQuery and there’s probably something in there that you can use for inspiration
whoami
whoamiOP2y ago
Hey Ian I believe the return type can probably be retrieved via ReturnType<NamedQuery<API, Name>> but I don't see NamedQuery being exported

Did you find this page helpful?