How do I import a type?

This might be something I'm just totally overlooking, but I'm used to prisma where you can just import any schema you've defined as a type. For example, if I have a table posts, I'd expecect to be able to import a Post type to be used in one of my child components. Is this possible with Drizzle? Or have you all found it to be unnecessary
11 Replies
Yatsuki
Yatsuki16mo ago
import from your .ts schema file ?
Will
WillOP16mo ago
@behalf but when I import, I'm importing the schema, not the type
Yatsuki
Yatsuki16mo ago
im not really good at ts but can't u just use the import type from "schema"
Will
WillOP16mo ago
@behalf I don't think so, can you provide an example? The schemas defined in schema.ts are not types, at least they don't appear to work like regular types when I try to use them.
ArwanceR
ArwanceR16mo ago
You want to use typescript interface of your defined table schema right?
fermentfan
fermentfan16mo ago
Do it like this:
export type myEntityType = InferModel<typeof myEntity>;
export type myEntityType = InferModel<typeof myEntity>;
myEntity being the table description in your schema.
Will
WillOP16mo ago
@arwancer yup exactly! @fermentfan oh thank you this is super helpful - I'm surprised that drizzle doesn't have a way to do this automatically like prismas generate
fermentfan
fermentfan16mo ago
I think it’s a fundamental architectural difference in drizzle vs Prisma as one works by using Typescript only, while Prisma is generating code in your project root.
Will
WillOP16mo ago
yeah that's fair, but I guess it would be nice to have a command that automatically generated all the types just by way of generating these lines : export type myEntityType = InferModel<typeof myEntity>; feature request I guess!
prateek98
prateek9816mo ago
I agree on this. Now the InferModel is also depreciated. 😢
T-Diggity
T-Diggity13mo ago
for anyone like me that got here from search: https://github.com/drizzle-team/drizzle-orm/releases/tag/0.28.3 Its deprecated, but you can now use InferSelectModel instead
GitHub
Release 0.28.3 · drizzle-team/drizzle-orm
UPDATE: The 0.28.3 release introduced a bug with types specifically for esm projects. Everything works as expected with cjs. If you're experiencing any type-related bugs as described in #1090, ...
Want results from more Discord servers?
Add your server