Skip to content

No typing error when passing a string or a number to data for update() #22445

Closed

Description

@yns01

Bug description

When calling update, any type can be passed to data:

prisma.user.update({
  where: {
    id: 1,
  },
  data: 10, // should trigger a type error
})

Null or Undefined trigger Typescript errors but strings or numbers don't.

There might be something wrong with the XOR type definition:

import { Prisma } from '@prisma/client'

type XOR<T, U> = T extends object
  ? U extends object
    ? (Without<T, U> & U) | (Without<U, T> & T)
    : U
  : T

type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never }

type TestType = XOR<Prisma.UserUpdateInput, Prisma.UserUncheckedUpdateInput>

const fn = (a: TestType) => {}
const fn1 = (a: Prisma.UserUpdateInput) => {}
const fn2 = (a: Prisma.UserUncheckedUpdateInput) => {}

fn(5) // No errors
fn1(5) // Type '5' has no properties in common with type 'UserUpdateInput'.ts(2559)
fn2(5) // Type '5' has no properties in common with type 'UserUncheckedUpdateInput'.ts(2559)

How to reproduce

I just followed the Getting started.

  1. Create model as below
  2. npx prisma generate

Expected behavior

A type error should be raised when anything but an object matching the table type is passed.

Prisma information

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

Environment & setup

  • OS: Any
  • Database: Any
  • Node.js version: 18.17.1

Prisma Version

prisma                  : 5.7.0
@prisma/client          : 5.7.0
Computed binaryTarget   : darwin
Operating System        : darwin
Architecture            : x64
Node.js                 : v18.17.1
Query Engine (Node-API) : libquery-engine 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Schema Engine           : schema-engine-cli 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9 (at node_modules/@prisma/engines/schema-engine-darwin)
Schema Wasm             : @prisma/prisma-schema-wasm 5.7.0-41.79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Default Engines Hash    : 79fb5193cf0a8fdbef536e4b4a159cad677ab1b9
Studio                  : 0.495.0

Activity

  1. added
    kind/bug
    on Dec 17, 2023
  2. added
    bug/1-unconfirmed
    topic: client types
    tech/typescript
    on Dec 20, 2023
  3. self-assigned this
    on Feb 16, 2024
  4. jkomyno commented on Feb 16, 2024

    @jkomyno
    Contributor

    Hi @yns01, both with [email protected] and our latest version, [email protected], I correctly. see the following output when running the snippet above:

    Invalid `prisma.user.update()` invocation in
    /.../index.ts:6:21
    
      3 async function main() {
      4   const prisma = new PrismaClient()
      5 
    → 6   await prisma.user.update({
            where: {
              id: 1
            },
            data: 1
                  ~
          })
    
    Argument `data`: Invalid value provided. Expected UserUpdateInput or UserUncheckedUpdateInput, provided Int.
    

    However, it seems that TypeScript Language Server on VSCode isn't able to display that there is a type mismatch. Thank you for opening this issue, we'll investigate.

  5. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Feb 16, 2024
  6. Jolg42 commented on Feb 16, 2024

    @Jolg42
    Contributor

    This looks like a duplicate of #9988, but since this is now confirmed & reproduced here, let's keep this one open and close the old one.

  7. changed the title [-]No typing error when passing a string or a number to data for update()[/-] [+]No typing error when passing a string or a number to data for `update()`[/+] on Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtech/typescripttopic: client typestopic: update()

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions