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

TypeScript Error TS2322 when assigning JavaScript Date object to Prisma DateTime field #20253

Closed
mmilardic opened this issue Jul 17, 2023 · 3 comments · Fixed by #20285
Closed
Assignees
Labels
5.0.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: clientExtensions topic: dates / DateTime topic: prisma-client
Milestone

Comments

@mmilardic
Copy link

Bug description

From Prisma v4.16.2, I am experiencing an issue when using Prisma Client extensions in combination with the Date type in TypeScript.

I created a custom factory to create User objects using the extended Prisma client (orm)

When attempting to use the Date object for the updated_at field, I am running into a TypeScript error (TS2322):
TS2322: Type 'Date' is not assignable to type 'Exact<string | Date, string | Date>'. Type 'Date' is not assignable to type '{ toString: {}; toDateString: never; toTimeString: never; toLocaleString: never; toLocaleDateString: never; toLocaleTimeString: never; valueOf: {}; getTime: never; getFullYear: never; getUTCFullYear: never; ... 33 more ...; [Symbol.toPrimitive]: never; }'. Types of property 'toDateString' are incompatible. Type '() => string' is not assignable to type 'never'. index.d.ts(1888, 5): The expected type comes from property 'updated_at' which is declared here on type 'Exact<{ id?: string | undefined; name: string; created_at?: string | Date | undefined; updated_at: string | Date; }, (Without<usersCreateInput, usersUncheckedCreateInput> & usersUncheckedCreateInput) | (Without<...> & usersCreateInput)'

It appears that the Date type isn't being correctly recognized or compatible with the extended Prisma client. I expect to be able to assign a JavaScript Date object to the updated_at field without a TypeScript error.
When using the prisma client without extensions, it works just fine.

This issue occurs in a minimal reproducible example (repository), which I have included as an attachment.

Environment:
Node.js: 18.16.0
TypeScript: 5.1.6
Prisma: 5.0.0

Looking forward to your support in resolving this issue. Thank you!

How to reproduce

  1. Go to repository
  2. Clone the repository
  3. Run the reproduction steps from README.md
  4. See error on build

Expected behavior

The expected behavior is that I should be able to assign a new Date() to the 'updated_at' field without TypeScript errors. This is because the 'updated_at' field in the Prisma schema is of type DateTime, which should be compatible with the JavaScript Date object. However, the current behavior shows a TypeScript error instead.

Prisma information

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
}

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

model users {
  id         String   @id @default(dbgenerated("public.uuid_generate_v4()")) @db.Uuid
  name       String
  created_at DateTime @default(dbgenerated("timezone('utc'::text, now())")) @db.Timestamp(6)
  updated_at DateTime @db.Timestamp(6)
}
import {
    PrismaClient,
} from '@prisma/client'

const prisma = new PrismaClient({
    log: ['query', 'info', 'warn'],
})

const orm = prisma.$extends({
    query: {
        $allModels: {
            $allOperations: async ({ args, model, operation, query }) => {
                    return await query(args)
            },
        },
    },
})

const UserFactory = {
    create: async (input?: Partial<Prisma.usersCreateInput>) => {
        return orm.users.create({
            data: {
                name: "name",
                updated_at: new Date(),
                ...input,
            },
        })
    },
}

Environment & setup

  • OS: macOS Ventura 13.0.1
  • Database: PostgreSQL 15.2
  • Node.js version: v18.16.0

Prisma Version

5.0.0
@mmilardic mmilardic added the kind/bug A reported bug. label Jul 17, 2023
@masterbater
Copy link

masterbater commented Jul 17, 2023

If you just need to update the date for updated_at, prisma has a builtin directive in schema

model Post {
  id        String   @id
  updatedAt DateTime @updatedAt
}

https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#updatedat

@vuki656
Copy link

vuki656 commented Jul 17, 2023

If you just need to update the date for updated_at, prisma has a builtin directive in schema

model Post {
  id        String   @id
  updatedAt DateTime @updatedAt
}

https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#updatedat

We use Liquibase for migrations and never modify Prisma schema except to change casing so that's not an option but thanks for the suggestion.

@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: prisma-client tech/typescript Issue for tech TypeScript. topic: dates / DateTime team/client Issue for team Client. topic: clientExtensions bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 17, 2023
@Jolg42 Jolg42 added this to the 5.1.0 milestone Jul 17, 2023
@aqrln aqrln added the 5.0.0 label Jul 17, 2023
@SevInf
Copy link
Contributor

SevInf commented Jul 19, 2023

I tested that this will be fixed by #20285

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0.0 bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. tech/typescript Issue for tech TypeScript. topic: clientExtensions topic: dates / DateTime topic: prisma-client
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants