chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
578 stars 43 forks source link

[BUG] Typescript errors on generated models #203

Closed EdwardDrapkin closed 7 months ago

EdwardDrapkin commented 7 months ago

Describe the bug *WhereUniqueInput and *DeleteArgsSchema seem to be erroneous in TS.

I've tried with generating from version 2.7.9, 2.8.1, and 3.0.0.

I've tried with zod 3.21.1 and zod 3.22.4

These are the errors I get:

 πŸ“’1123 🍺 tsc schemas/outputTypeSchemas/UserDeleteArgsSchema.ts
inputTypeSchemas/UserWhereUniqueInputSchema.ts:7:14 - error TS2322: Type 'ZodIntersection<ZodObject<{ id: ZodNumber; }, "strip", ZodTypeAny, { id?: number; }, { id?: number; }>, ZodObject<{ id: ZodOptional<ZodNumber>; AND: ZodOptional<...>; OR: ZodOptional<...>; NOT: ZodOptional<...>; phone: ZodOptional<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'ZodType<UserWhereUniqueInput, ZodTypeDef, UserWhereUniqueInput>'.
  Types of property '_type' are incompatible.
    Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type 'UserWhereUniqueInput'.
      Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type '{ id: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }'.
        Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type '{ id: number; }'.
          Property 'id' is optional in type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' but required in type '{ id: number; }'.

7 export const UserWhereUniqueInputSchema: z.ZodType<Prisma.UserWhereUniqueInput> = z.object({
               ~~~~~~~~~~~~~~~~~~~~~~~~~~

outputTypeSchemas/UserDeleteArgsSchema.ts:12:14 - error TS2322: Type 'ZodObject<{ select: ZodOptional<ZodType<UserSelect<DefaultArgs>, ZodTypeDef, UserSelect<DefaultArgs>>>; where: ZodType<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<UserDeleteArgs<DefaultArgs>, ZodTypeDef, UserDeleteArgs<DefaultArgs>>'.
  Types of property '_type' are incompatible.
    Type '{ select?: UserSelect<DefaultArgs>; where?: UserWhereUniqueInput; }' is not assignable to type 'UserDeleteArgs<DefaultArgs>'.
      Property 'where' is optional in type '{ select?: UserSelect<DefaultArgs>; where?: UserWhereUniqueInput; }' but required in type 'UserDeleteArgs<DefaultArgs>'.

12 export const UserDeleteArgsSchema: z.ZodType<Prisma.UserDeleteArgs> = z.object({
                ~~~~~~~~~~~~~~~~~~~~

Found 2 errors in 2 files.

Errors  Files
     1  inputTypeSchemas/UserWhereUniqueInputSchema.ts:7
     1  outputTypeSchemas/UserDeleteArgsSchema.ts:12

Screenshots If applicable, add screenshots to help explain your problem.

Package versions (please complete the following information):

Additional context Here's a repro repo: zpt-repro.zip just run npm install and then npx prisma generate --schema=./schema.prisma and tsc schemas/outputTypeSchemas/UserDeleteArgsSchema.ts will output TS errors.

chrishoermann commented 7 months ago

@EdwardDrapkin that is interesting - Did this issue resolve becuse I can't confirm this type error on my end. I tried the version combinations you mentioned in your provided repo and all of them where able to compile without errors.

EdwardDrapkin commented 7 months ago

I attached a small repro, but with this package.json:


  "dependencies": {
    "@prisma/client": "^5.5.2",
    "prisma": "^5.5.2",
    "zod": "^3.21.1",
    "zod-prisma-types": "3.0.0"
  },

and this schema:

generator zod {
  isGenerateSelect  = true
  isGenerateInclude = true
  provider          = "zod-prisma-types"
  output            = "./schemas"
  useMultipleFiles  = true
  writeBarrelFiles  = false
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["fullTextSearch"]
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL_LEGACY")
}

model User {
  //
  id    Int    @id @default(autoincrement()) @db.UnsignedInt
  phone String @db.VarChar(50)

  @@map(name: "users")
}

I get the errors - and I've had a coworker confirm they get the same issue. I'm on TS 5.2.2 if that matters.

For what it's worth, the TS errors only appear when I specifically compile schemas/outputTypeSchemas/UserDeleteArgsSchema.ts:


    πŸ•˜ 16:06:13 βŽ¦πŸ“˜ edward:~/projects/zpt-repro/schemas  ⎑
 πŸ“’1125 🍺 tsc outputTypeSchemas/UserDeleteArgsSchema.ts
inputTypeSchemas/UserWhereUniqueInputSchema.ts:7:14 - error TS2322: Type 'ZodIntersection<ZodObject<{ id: ZodNumber; }, "strip", ZodTypeAny, { id?: number; }, { id?: number; }>, ZodObject<{ id: ZodOptional<ZodNumber>; AND: ZodOptional<...>; OR: ZodOptional<...>; NOT: ZodOptional<...>; phone: ZodOptional<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>>' is not assignable to type 'ZodType<UserWhereUniqueInput, ZodTypeDef, UserWhereUniqueInput>'.
  Types of property '_type' are incompatible.
    Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type 'UserWhereUniqueInput'.
      Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type '{ id: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }'.
        Type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' is not assignable to type '{ id: number; }'.
          Property 'id' is optional in type '{ id?: number; } & { id?: number; AND?: UserWhereInput | UserWhereInput[]; OR?: UserWhereInput[]; NOT?: UserWhereInput | UserWhereInput[]; phone?: string | StringFilter<...>; }' but required in type '{ id: number; }'.

7 export const UserWhereUniqueInputSchema: z.ZodType<Prisma.UserWhereUniqueInput> = z.object({
               ~~~~~~~~~~~~~~~~~~~~~~~~~~

outputTypeSchemas/UserDeleteArgsSchema.ts:12:14 - error TS2322: Type 'ZodObject<{ select: ZodOptional<ZodType<UserSelect<DefaultArgs>, ZodTypeDef, UserSelect<DefaultArgs>>>; where: ZodType<...>; }, "strict", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<UserDeleteArgs<DefaultArgs>, ZodTypeDef, UserDeleteArgs<DefaultArgs>>'.
  Types of property '_type' are incompatible.
    Type '{ select?: UserSelect<DefaultArgs>; where?: UserWhereUniqueInput; }' is not assignable to type 'UserDeleteArgs<DefaultArgs>'.
      Property 'where' is optional in type '{ select?: UserSelect<DefaultArgs>; where?: UserWhereUniqueInput; }' but required in type 'UserDeleteArgs<DefaultArgs>'.

12 export const UserDeleteArgsSchema: z.ZodType<Prisma.UserDeleteArgs> = z.object({
                ~~~~~~~~~~~~~~~~~~~~

Found 2 errors in 2 files.

Errors  Files
     1  inputTypeSchemas/UserWhereUniqueInputSchema.ts:7
     1  outputTypeSchemas/UserDeleteArgsSchema.ts:12

I'd be happy to hop in a Zoom and screen share if that helps.

chrishoermann commented 7 months ago

@EdwardDrapkin can you please provide your tsconfig. Maybe some options there cause the issue. Here's the confit I use in my test repo:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": ".",
    "lib": ["dom", "ESNext"],
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "module": "CommonJS",
    "target": "ESNext",
    "moduleResolution": "node",
    "isolatedModules": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "allowJs": false,
    "resolveJsonModule": true,
    "removeComments": true,
    "forceConsistentCasingInFileNames": true,
    "inlineSources": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noPropertyAccessFromIndexSignature": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "useDefineForClassFields": true,
    "useUnknownInCatchVariables": true,
    "types": ["vitest/globals"]
  },
  "include": ["prisma/generated/**/*.ts", "src/**/*.ts"]
}
chrishoermann commented 7 months ago

@EdwardDrapkin it seems that you have to set strictNullChecks: true in your tsconfig.json. when I disable it in my test repo I can reporduce the error. closing since this should fix your problem.

EdwardDrapkin commented 7 months ago

Thank you!