Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: The inferred type of X cannot be named without a reference to ../../../../../node_modules/drizzle-zod/schema.types.internal.mjs #3732

Closed
1 task done
thedevdavid opened this issue Dec 10, 2024 · 28 comments
Labels
bug/cant-reproduce Lack steps to fully reproduce. A repository for reproduction may be necessary bug Something isn't working drizzle/zod priority Will be worked on next

Comments

@thedevdavid
Copy link

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.38.0

What version of drizzle-kit are you using?

0.30.0

Other packages

[email protected]

Describe the Bug

Since the latest update, all previously working drizzle-zod functions show a type error.

The inferred type of updateUserProfileSchema cannot be named without a reference to ../../../../../node_modules/drizzle-zod/schema.types.internal.mjs

image image

Node.js v22.10.0
Bun v1.1.38
Turborepo v2.3.3

Workaround is disabling declaration and declarationMap in tsconfig.json:

"declaration": false,
 "declarationMap": false
@thedevdavid thedevdavid added the bug Something isn't working label Dec 10, 2024
@jikyu
Copy link
Contributor

jikyu commented Dec 11, 2024

Running into this as well, and the aforementioned workaround is not working either

"declaration": false,
"declarationMap": false

@trevorpfiz
Copy link

Running into this as well, and the aforementioned workaround is not working either

"declaration": false,
"declarationMap": false
"compilerOptions": {
    "declaration": false,
    "declarationMap": false,
    "emitDeclarationOnly": false,
    "noEmit": true
  },

this seems to work for me

@jikyu
Copy link
Contributor

jikyu commented Dec 14, 2024

Would love to solve this without setting declaration to false as it would break other parts of the repo. Also see related issue for drizzle-valibot here: #3747

@trevorpfiz
Copy link

trevorpfiz commented Dec 15, 2024

Running into this as well, and the aforementioned workaround is not working either

"declaration": false,
"declarationMap": false
"compilerOptions": {
    "declaration": false,
    "declarationMap": false,
    "emitDeclarationOnly": false,
    "noEmit": true
  },

this seems to work for me

getting this error in my api when I try to use this:

messages: t
      .jsonb()
      .array()
      .$type<CoreMessage[]>()
      .notNull()
      .default(sql`'{}'::jsonb[]`),

@SpeedoPasanen
Copy link

Happened to me after updating drizzle-zod from ^0.5.1 to ^0.6.0. Reverting back to 0.5.1 fixes it.

Breaks all createSelectSchema calls and any types that are inferred from them.
These compilerOptions did nothing but broke my Angular build (alrhough "declaration": false I did already have):

"compilerOptions": {
    "declaration": false,
    "declarationMap": false,
    "emitDeclarationOnly": false,
    "noEmit": true
  },

@tconroy
Copy link

tconroy commented Dec 26, 2024

+1, also running into this issue...

import type { InferInsertModel, InferSelectModel } from "drizzle-orm";
import {
  createInsertSchema,
  createSelectSchema,
  createUpdateSchema,
} from "drizzle-zod";

import { CallToAction } from "@acme/schema/call-to-action";

export type TCallToActionInsertSchema = InferInsertModel<typeof CallToAction>;
export type TCallToActionSelectSchema = InferSelectModel<typeof CallToAction>;
export type TCallToActionUpdateSchema = Partial<TCallToActionInsertSchema>;

export const ZCallToActionInsertSchema = createInsertSchema(CallToAction);
export const ZCallToActionSelectSchema = createSelectSchema(CallToAction);
export const ZCallToActionUpdateSchema = createUpdateSchema(CallToAction);

all of the returns from the create* calls trigger error:

The inferred type of 'ZCallToActionInsertSchema' cannot be named without a reference to '../../../../node_modules/drizzle-zod/schema.types.internal.mjs'. This is likely not portable. A type annotation is necessary.

When I try the compilerOptions.declarations changes, it fixes in that particular package (in my case, my @acme/validators package), but inference is broken for all other apps/packages that import it.

@issacclee
Copy link

Same here with "drizzle-orm": "^0.38.3" and "drizzle-zod": "^0.6.1".
Tried to add {"declaration": false, "declarationMap": false} to tsconfig, but error persists.

@ismoiliy98
Copy link

Do you happen to have any updates on this? Is anyone working on PR to fix it?

@Abror4544
Copy link

Any updates?

@Abror4544
Copy link

Abror4544 commented Jan 2, 2025

Workaround without disabling declaration:

export const userInsertSchema = createInsertSchema(user).extend({});
export const userUpdateSchema = createUpdateSchema(user).extend({});
export const userSelectSchema = createSelectSchema(user).extend({});

export type UserInsert = z.infer<typeof userInsertSchema>;
export type UserUpdate = z.infer<typeof userUpdateSchema>;
export type UserSelect = z.infer<typeof userSelectSchema>;

@SpeedoPasanen
Copy link

SpeedoPasanen commented Jan 3, 2025

Workaround without disabling declaration:

export const userInsertSchema = createInsertSchema(user).extend({});

Awesome, almost worked for me. I have a tree, where a table references itself and has a children column of it's own type. This seems to be the only error remaining for me now with 0.6.1 using this .extend({}) hack:

TS2615: Type of property 'children' circularly references itself in mapped type '{ [k in keyof { virtual?: ZodTypeAny | ZodBoolean | undefined; tagName: ZodString; attributes: ZodObject<{ [x: string]: ZodTuple<[any, ...any[]], null>; }, "strip", ZodTypeAny, { ...; }, { ...; }>; content?: ZodTypeAny | ... 1 more ... | undefined; children?: ZodTypeAny | ... 1 more ... | undefined; data?: ZodTuple<...'

Edit: Looks like this is from a PG JSONB column in a table, which is typed using $type<MyTreeModel>() as a tree with children referencing the same MyTreeModel type.

Edit 2: Workaround: Type the JSONB column as any for backend, and type it manually for client.

export const tLetters = pgTable(
  'letters',
  {
    body: jsonb('body').$type<any[]>().notNull().default([]),
// ...
export type Letter = Omit<typeof tLetters.$inferSelect, 'body'> & {
  body: LetterBody;
};

@wbmrcb
Copy link

wbmrcb commented Jan 7, 2025

I got rid of this error by refining the field,

const sampleSchema = createSelectSchema(sampleTable, {
    jsonbField: z.object({})
})

@slimshreydy
Copy link

Workaround without disabling declaration:

export const userInsertSchema = createInsertSchema(user).extend({});
export const userUpdateSchema = createUpdateSchema(user).extend({});
export const userSelectSchema = createSelectSchema(user).extend({});

export type UserInsert = z.infer<typeof userInsertSchema>;
export type UserUpdate = z.infer<typeof userUpdateSchema>;
export type UserSelect = z.infer<typeof userSelectSchema>;

This worked great for me for anything without JSON fields.

I also had a few JSON fields so @wbmrcb's suggestion helped for those too.

Would love to see a proper fix to this soon though!

@L-Mario564
Copy link
Collaborator

Can someone provide steps to reproduce this bug? Mainly want a table and schema definition that causes this error alongside the full tsconfig.json file; as otherwise, I'm unable to reproduce.

@kedom1337
Copy link

kedom1337 commented Jan 9, 2025

Just ran into this aswell. The aforementioned workarounds either dont work or are not viable with my setup. Would love to see this fixed. Using:

"drizzle-valibot": "^0.3.1",
"drizzle-orm": "^0.38.3",
"valibot": "^1.0.0-beta.11"

@L-Mario564
Copy link
Collaborator

@kedom1337 Could you provide more info on how to reproduce this bug? (Read the comment above yours to see what I'm looking for).

@Jac0xb
Copy link

Jac0xb commented Jan 9, 2025

So much weird behavior, in addition to the inferred type issue. Also having a import ./column.mjs outputted in my .d.ts file. Definitely something jank happening in this distribution as all the other drizzle packages work perfectly fine.

@L-Mario564 L-Mario564 added the bug/cant-reproduce Lack steps to fully reproduce. A repository for reproduction may be necessary label Jan 10, 2025
@slimshreydy
Copy link

slimshreydy commented Jan 11, 2025

@L-Mario564 I just made a repro repo with a minimal example. Seems the issue shows up with drizzle-zod when using declaration: true with module: esnext and moduleResolution: bundler (typical settings for say, a Nextjs project). I use these typescript config setting because I use drizzle-zod as part of a ORM package I publish for internal use with Nextjs projects, so I need the TS bindings provided by declaration: true.

You can find the repo here. Let me know if you run into issues replicating the bug using this:
https://github.com/slimshreydy/drizzle-zod-ts-bug.git

@jikyu
Copy link
Contributor

jikyu commented Jan 11, 2025

I think the common thread here is that this seems to happen in monorepo setups

@slimshreydy
Copy link

@jikyu I mean my example repo above isn't a monorepo -- it's a dead simple repo with 2 files. It occurs whenever emitting declarations on a project with module = esnext and moduleResolution = bundler.

@Rick-Phoenix
Copy link

Rick-Phoenix commented Jan 13, 2025

@slimshreydy I am getting this error in that exact same situation, although I am also in a monorepo and using declaration on that specific package (it's a package that is used to share types and schemas between backend and frontend)

Reverting back to 0.5.1 also solved the issue for me (thanks @SpeedoPasanen )

@L-Mario564
Copy link
Collaborator

@slimshreydy Thank you for providing the repo! Will take this from here.

@slimshreydy
Copy link

@Rick-Phoenix yep this seems to be an issue unique to 0.6.0+. That being said, 0.6.0+ adds support for arrays (finally!) so I really want to upgrade to that asap!

@MarcSustainableShift
Copy link

MarcSustainableShift commented Jan 14, 2025

I had a similar issue with trpc and drizzle after adding a json-typed column (postgres, pg-core).

The inferred type of 'testRouter' cannot be named without a reference to '../../../../../../../node_modules/drizzle-zod/utils.d.mts'. This is likely not portable. A type annotation is necessary.

Minimum example:
drizzle schema:

export const TestTable = schema.table(
  "test_table",
  {
    id: serial("id").primaryKey(),
    metadata: json("metadata"),
  },
);

trpc router:

const testInsertSchema = createInsertSchema(TestTable);

export const testRouter = {
  insert: publicProcedure
    .input(testInsertSchema)
    .mutation(....)
} satisfies TRPCRouterRecord;

Which I could resolve by explicitly defining the json-column as a zod-object:

const actionResultInsertSchema = createInsertSchema(ActionResult, {
  metadata: z.object({}).optional(),
});

As there are already multiple comments in this thread with json examples, this might be a common cause.

@Rick-Phoenix
Copy link

In my case I did not have any json columns in my tables

@AndriiSherman
Copy link
Member

Available in [email protected]

Release Notes

@thedevdavid
Copy link
Author

Available in [email protected]

Release Notes

Thanks @AndriiSherman ! Tho it seems like 0.7.0 has the same issues. As mentioned by others too in #3645 (comment) , #4026 , and possibly #4016.

For anyone coming across this: Downgrading drizzle-zod to 0.5.1 works.

@slimshreydy
Copy link

Fwiw I have not had this same issue; upgrading to 0.7.0 fixed it for me.

Here's my tsconfig for reference. (outDir/declaration properties are set because I used drizzle-zod as part of a package I publish for internal use)

{
  "compilerOptions": {
    "lib": ["esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "incremental": true,
    "target": "es2017",
    "outDir": "./dist",
    "declaration": true,
    "declarationDir": "./dist",
    "declarationMap": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/cant-reproduce Lack steps to fully reproduce. A repository for reproduction may be necessary bug Something isn't working drizzle/zod priority Will be worked on next
Projects
None yet
Development

No branches or pull requests