How to build Drizzle explicit types
Call me dumb, but I don't fully get how we can have explicit types that we can easily
import
in the various components of the application.
I've worked few months with Prisma, and over there you have the engine that's able to generate all the types that your queries will generate and you can import them as needed.
As far as I can see, Drizzle ORM does not do that. While I feel that what it does is still great, they provide you the type
definition with all the props properly typed but you don't have a way to import them.
You rely on the implicit types that the query will expose.
Don't you think that's kind of limiting your ability to have explicit types that you can import in your components?
A quick example
Let's say you have a component that display the values from the following query:
If I hover on the function name with VS Code I get the following message:
While this is semantically correct, how do you type the prop inside the component that will consume the result from this query?
Are you doing something like this?
I mean, doesn't it feel "too much"?
An approach with drizzle-zod
I've started to use drizzle-zod
since I already use zod
inside my application, and now in each of my schemas I add the following:
And then I find myself generating the types inside my index.d.ts
file like so:
So now I can go back to the previous <DisplayRole />
component and import the Role
type.
Do you have a better approach to make TypeScript fully working? Is probably my project not configured well enough to work with Drizzle?2 Replies
Instead of
drizzle-zod
, you can also use the built-in type API in drizzle-orm
: https://orm.drizzle.team/docs/goodies
Then explicitly define the return type with the RolesSelect
type:
When you pass the result to a prop, check for undefined. Either handle it in the parent component before you pass the prop or within your component.
You can then type the prop using the RolesSelect type as well.
or:
Let me know if this helps!Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks for your help, on mobile now so just wanted to thank you as soon as I saw this.
I'll get back to you once I'll be working with a keyboard 😅