[BUG]: `"composite" true` and Yarn workspace prevents `pgTable` from being compiled
Problem
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. <details> <summary> schema.ts </summary> [code block] </details> [code block] 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_
Error Output
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.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix TypeScript Compilation Error with Composite in Yarn Workspace
The TypeScript compiler is unable to infer the type of the 'users' table due to the lack of an explicit type annotation and the use of 'composite' in the tsconfig.json. This results in a non-portable type reference that causes compilation errors when exporting types from a Yarn workspace.
Awaiting Verification
Be the first to verify this fix
- 1
Add Explicit Type Annotation
Modify the 'schema.ts' file to include an explicit type annotation for the 'users' table. This will help TypeScript understand the type without needing to reference the internal module directly.
typescriptconst users: TableType = pgTable('users', { /* table definition */ }); - 2
Update tsconfig.json for Backend
Ensure that the 'tsconfig.json' in the 'backend' package has the 'composite' option set to true, which is necessary for TypeScript project references.
json{ "compilerOptions": { "composite": true, /* other options */ } } - 3
Use TypeScript Project References
Make sure that the 'frontend' package references the 'backend' package correctly in its own 'tsconfig.json'. This ensures that TypeScript knows about the types exported from 'backend'.
json{ "references": [{ "path": "../backend" }] } - 4
Rebuild the Backend Package
After making the changes, run the TypeScript compiler in the 'backend' package to ensure that the types are correctly compiled and exported.
bashtsc --build - 5
Test the Frontend Package
Finally, run the TypeScript compiler in the 'frontend' package to verify that the types from 'backend' are correctly recognized and that there are no compilation errors.
bashtsc --build
Validation
Check that both the 'backend' and 'frontend' packages compile without errors. Specifically, ensure that the 'users' table type is recognized in the 'frontend' package without any TypeScript errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep