blitz-js / legacy-framework

MIT License
2 stars 2 forks source link

Type Error in `create mutation` function #163

Closed winner106 closed 2 years ago

winner106 commented 2 years ago

What is the problem?

Type Error in app\admin\products\mutations\createProduct.ts , I modified the CreateProduct , but the param data Type Error

Paste all your error logs here:

(property) data: (Prisma.Without<Prisma.ProductCreateInput, Prisma.ProductUncheckedCreateInput> & 
Prisma.ProductUncheckedCreateInput) | (Prisma.Without<Prisma.ProductUncheckedCreateInput, Prisma.ProductCreateInput> & 
Prisma.ProductCreateInput)
The data needed to create a Product.

Type '{ name?: string; secretKey?: string; description?: string; status?: boolean; }' is not assignable to type 
'(Without<ProductCreateInput, ProductUncheckedCreateInput> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput)'.
  Type '{ name?: string; secretKey?: string; description?: string; status?: boolean; }' is not assignable to type 
'Without<ProductUncheckedCreateInput, ProductCreateInput> & ProductCreateInput'.
    Type '{ name?: string; secretKey?: string; description?: string; status?: boolean; }' is not assignable to type 'ProductCreateInput'.
      Property 'name' is optional in type '{ name?: string; secretKey?: string; description?: string; status?: boolean; }' but required in type 'ProductCreateInput'.ts(2322)
index.d.ts(4547, 5): The expected type comes from property 'data' which is declared here on type
 '{ select?: ProductSelect;    include?: ProductInclude;    data: (Without<ProductCreateInput, ProductUncheckedCreateInput> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput); }'

What are detailed steps to reproduce this?

1.modified then \db\schema.prisma , add model Product as follows:

model Product {
  id          Int      @id @default(autoincrement())
  name        String   @unique
  secretKey   String   @unique
  description String?
  status      Boolean? @default(true)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  orders      Order[]
}
  1. run blitz prisma db push
  2. run blitz generate crud product
  3. modified the file CreateProduct.ts , as follows:
    
    import { resolver } from "blitz"
    import db from "db"
    import { z } from "zod"

const CreateProduct = z.object({ name: z.string(), secretKey: z.string(), description: z.string().optional(), status: z.boolean().optional(), })

export default resolver.pipe(resolver.zod(CreateProduct), resolver.authorize(), async (input) => { // TODO: in multi-tenant app, you must add validation to ensure correct tenant const product = await db.product.create({ data: input })

return product })


### Run `blitz -v` and paste the output here:

```ps
Windows 10 | win32-x64 | Node: v16.13.1

blitz: 0.45.0 (global)
blitz: 0.44.4 (local)

  Package manager: yarn
  System:
    OS: Windows 10 10.0.19043
    CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
    Memory: 15.42 GB / 23.87 GB
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD
    npm: 7.24.2 - ~\AppData\Local\Yarn\bin\npm.CMD
    Watchman: Not Found
  npmPackages:
    @prisma/client: 3.7.0 => 3.7.0
    blitz: 0.44.4 => 0.44.4
    prisma: 3.7.0 => 3.7.0
    react: 18.0.0-beta-149b420f6-20211119 => 18.0.0-beta-149b420f6-20211119
    react-dom: 18.0.0-alpha-5ca4b0433-20211020 => 18.0.0-alpha-5ca4b0433-20211020
    typescript: ~4.5 => 4.5.4```

Please include below any other applicable logs and screenshots that show your problem:

\db\schema.prisma image

app\admin\products\mutations\createProduct.ts image

node_modules\.prisma\client\index.d.ts image

beerose commented 2 years ago

Hi @winner106, I tried reproducing it but it works fine for me — I don't see a TypeScript error and the types are correct:

Screenshot 2022-01-18 at 09 57 54

Is it only in VS Code or do you see it in terminal logs as well? If it's the former, you can try restarting the TS Server in VS Code. Otherwise, if that's still a problem could you provide a repo with reproduction?

ameshkin commented 1 year ago

I am also having this issue. I was able to get past this by editing my tsconfig.json file

I set this to false "strictNullChecks": false,

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "baseUrl": "src",
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "strictNullChecks": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "noUncheckedIndexedAccess": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "tsBuildInfoFile": ".tsbuildinfo"
  },
  "exclude": ["node_modules", "**/*.e2e.ts", "cypress"],
  "include": ["blitz-env.d.ts", "**/*.ts", "**/*.tsx", "types"],
}