drizzle-orm: [BUG]: `"composite" true` and Yarn workspace prevents `pgTable` from being compiled
What version of drizzle-orm are you using?
0.26.1
What version of drizzle-kit are you using?
0.18.1
Describe the Bug
TL;DR
TypeScript can’t compile simple schema.ts file with users example table when using "composite": true in tsconfig.json
Longer version
I have Yarn workspace with two packages, let’s call them backend and frontend.
Naturally, frontend depends on backend. For that to work, backend’s tsconfig.json must include "composite": true option.
backend uses drizzle-orm internally for migrations and I would like to use a standalone schema.ts file where all of the tables would be declared. With such setup, I would need to export said tables to use them elsewhere in the code.
This is the moment where export before the users table declaration makes TypeScript compiler think that anyone can import users (type of which depends on inference of drizzle-orm package), even outside of the backend package. Due to lack of export(package) in TypeScript, this leads to compiler errors.
schema.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: serial("id").primaryKey(),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
});
- error TS2742: The inferred type of 'users' cannot be named without a reference to '../../node_modules/drizzle-orm/db.d-2eb7c122'. This is likely not portable. A type annotation is necessary.
3 export const users = pgTable("users", {
~~~~~
Found 1 error.
error Command failed with exit code 1.
Expected behavior
Everything compiles just fine.
I found possible workarounds, but none of them are optimal: https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189
Environment & setup
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 4
- Comments: 23 (9 by maintainers)
Yes it does. But it should work with any setting. I need all of them to be
trueshould be reopened, I don’t think my PR fixed it completely.
I’ve got a pretty similar error for relations.
Literally adding
exportin front ofRelations,One, andManyinsidequery-promise.dresolved the issue.I finally looked into it and workaround #<span/>3.1 of https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 could also be applied (
PgTableWithColumnsmust be made available by exporting it), that would fix it in the library itself.See https://github.com/pkerschbaum/drizzle-repro/compare/main...fix/patch-for-drizzle-orm for the changes.
I would suggest that drizzle-orm applies this to easen the burden of pnpm users.
I just accidentally found an workaround, adding the line to any file with the error, fixes it for me. @TmLev can you validate?
Thanks for the info. However that is a change, not everyone can do!
I don’t think you should do that, but if it fixes the error…