mongoose-zod icon indicating copy to clipboard operation
mongoose-zod copied to clipboard

Types not working

Open DHFW opened this issue 1 year ago • 3 comments

I tried the basic example but types are not recognized. Using Node 18 mongoose-zod: 0.1.1 mongoose: 6.6.5 zod 3.22.4

export const zodSchema = z
  .object({
    tenantId: z.string(), // make unique
    name: z.string(),
  })
  .mongoose({
    schemaOptions: {
      collection: 'MyModel',
    },
    typeOptions: {
      tenantId: { unique: true },
    },
  })

export const MyModel = mongoose.model('MyModel', toMongooseSchema(zodSchema))
const myModel = new MyModel({ ten })

I would expect auto complete support on the last line, it should complete the ten with tenant, right?

Btw, with skipLibCheck: false I get some errors:

node_modules/zod/lib/types.d.ts:490:22 - error TS2428: All declarations of 'ZodObject' must have identical type parameters.

490 export declare class ZodObject<T extends ZodRawShape, UnknownKeys extends UnknownKeysParam = UnknownKeysParam, Catchall extends ZodTypeAny = ZodTypeAny, Output = objectOutputType<T, Catchall, UnknownKeys>, Input = objectInputType<T, Catchall, UnknownKeys>> extends ZodType<Output, ZodObjectDef<T, UnknownKeys, Catchall>, Input> {
                         ~~~~~~~~~

node_modules/mongoose-zod/dist/index.d.ts:1:8 - error TS1192: Module '"/Users/didierhartong/Projects/Stedin/lsis-functions/node_modules/zod/index"' has no default export.

1 import z$1, { z, ZodObject, ZodTypeAny } from 'zod';
         ~~~

  node_modules/zod/index.d.ts:1:1
    1 export * from "./lib";
      ~~~~~~~~~~~~~~~~~~~~~~
    'export *' does not re-export a default.

node_modules/mongoose-zod/dist/index.d.ts:34:15 - error TS2428: All declarations of 'ZodObject' must have identical type parameters.

But when I set skipLibCheck: true those errors are gone, but this may be related to the issue?

DHFW avatar Dec 20 '23 11:12 DHFW

Hello, could you try with zod 3.20.x? They definitively have changed something significant-ish since that version - I even have some of my zod schemas broken in 3.22.

On the other hand, this might be related to skipLibCheck: false because I always set it to true in all of my projects.

andreww2012 avatar Dec 21 '23 07:12 andreww2012

Hi, I tried it with Zod 3.20.0. Clean NPM install (deleted node_modules and also deleted package-lock.json). skipLibCheck: true. Makes no difference. NPM version 10.2.0. I checked with skipLibCheck: false and with that zod version it also get the same errors as mentioned above. But just to understand, I should be expecting autocomplete for properties when i.e. creating a new model, right?

DHFW avatar Dec 21 '23 09:12 DHFW

Could you please create a repo with a minimal reproduction of this issue? It is a bit hard to tell what is causing it from the current input.

I should be expecting autocomplete for properties when i.e. creating a new model, right?

Yes, certainly.

andreww2012 avatar Dec 21 '23 10:12 andreww2012