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

Build fails with Prisma 4.11.0 due to new toFixed method on DecimalJsLike interface. #90

Closed
sethmiller opened this issue Mar 1, 2023 · 6 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request todo Needs to be implemented

Comments

@sethmiller
Copy link

sethmiller commented Mar 1, 2023

I looked in the issues list and didn't find a duplicate of this issue.

Issue

Prisma added a toFixed method to the DecimalJsLike interface. This causes compilation to fail when using zod-prisma-types with the following error.

prisma/generated/zod/index.ts:463:14 - error TS2322: Type 'ZodObject<{ equals: ZodOptional<ZodEffects<ZodUnion<[ZodNumber, ZodString, ZodObject<{ d: ZodArray<ZodNumber, "many">; e: ZodNumber; s: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>]>, string | ... 1 more ... | { ...; }, string | ... 1 more ... | { ...; }>>; ... 11 more ...; _max: ZodOptional<...>; }, "stri...' is not assignable to type 'ZodType<NestedDecimalWithAggregatesFilter, ZodTypeDef, NestedDecimalWithAggregatesFilter>'.
  The types of '_type.equals' are incompatible between these types.
    Type 'string | number | { d: number[]; e: number; s: number; } | undefined' is not assignable to type 'string | number | Decimal | DecimalJsLike | undefined'.

This error is resolved by rolling Prisma back to 4.10.0.

Reproduction

I made a repo to reproduce the issue but it should work with any schema that has a Decimal column.

For convenience, the schema.prisma file is

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

generator zod {
  provider = "zod-prisma-types"
}

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

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  num   Decimal
}
@chrishoermann
Copy link
Owner

@sethmiller Thanks for the report. I quickly looked into this and it seems that this is more work than I initially expected. Simply adding a toFixed: z.function().return(z.string) seems not to work because then Typescript somehow requires the Prisma.Decimal type with all the other 94 methods and does not simply accept every Object that meets the requirements of the interface.

I' don't know when I have the time to get to it but I'll definitly address this.

@chrishoermann chrishoermann self-assigned this Mar 3, 2023
@chrishoermann chrishoermann added bug Something isn't working enhancement New feature or request todo Needs to be implemented labels Mar 3, 2023
@sethmiller
Copy link
Author

No worries. Thanks for the quick reply!

@chrishoermann
Copy link
Owner

@sethmiller I have a fix for this problem that generally works but one of my tests is failing. If I try to pass an object like

export const decimalJsLikeOne: DecimalJsLike = {
  d: [1230000], // array of digits
  e: -1, // exponent
  s: 1, // sign
  toFixed: () => '1230000',
};

via trpc it seems that the to fixed method gets stripped out. I don't know why but it works with new Decimal(), new Prisma.Decimal(), numbers and strings. so the main usecases should be covered.

can you please try it out on your end with 2.4.1-beta.0 before I release it into the world?

@sethmiller
Copy link
Author

When testing against my test repo, it is not able to resolve DecimalJsLike.

prisma/generated/zod/index.ts:11:45 - error TS2552: Cannot find name 'DecimalJsLike'. Did you mean 'DecimalJSLike'?

11 export const DecimalJSLikeSchema: z.ZodType<DecimalJsLike> = z.object({ d: z.array(z.number()), e: z.number(), s: z.number(), toFixed: z.function().args().returns(z.string()), });

The message is a little confusing since there is the type DecimalJSLike (with a capital S) is also defined in the file. That said, prefixing with Prisma (e.g. Prisma.DecimalJsLike) or importing DecimalJsLike (lowercase "s") from Prisma resolves the issue.

I haven't been able to test it beyond this simple use case, however (I haven't incorporated this package into a larger project just yet).

@chrishoermann
Copy link
Owner

Thanks for testing it out. I'll refine this a bit before officially releasing it.

chrishoermann added a commit that referenced this issue Mar 10, 2023
@chrishoermann
Copy link
Owner

@sethmiller should be fixed now in the latest release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request todo Needs to be implemented
Projects
Development

No branches or pull requests

2 participants