question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma's Decimal is incompatible with decimal.js

See original GitHub issue

Bug description

Prisma client bundles decimal.js and exports the class Decimal. Unfortunately, that class cannot be used with the Decimal class imported directly from decimal.js, because both classes declare toStringTag.

Example:

import { Decimal } from "decimal.js"
import { Prisma } from "@prisma/client"

const test = new Decimal(new Prisma.Decimal("12.0"))
// FYI: const test2 = new Prisma.Decimal(new Decimal("12.0")) also gives a type error

Gives the following error

Argument of type ‘Decimal’ is not assignable to parameter of type ‘Value’. Type ‘import(“*/node_modules/@prisma/client/runtime/index").Decimal’ is not assignable to type 'import("/node_modules/decimal.js/decimal”).default’. Types have separate declarations of a private property ‘toStringTag’.

How to reproduce

  1. Install decimal.js 10.4.2 and @prisma/client 4.6.1.
  2. The following gives a type error in typescript (tested with 4.8.4):
import { Decimal } from "decimal.js"
import { Prisma } from "@prisma/client"

const test = new Decimal(new Prisma.Decimal(12.0))
// FYI: const test2 = new Prisma.Decimal(new Decimal("12.0")) also gives a type error

Expected behavior

The Decimals from Prisma and decimal.js should be compatible.

This issue is particularly painful when working with mathjs (which depends on decimal.js). The BigNumber type from mathjs (which is just reexported Decimal from decimal.js, is also incompatible with the Prisma’s Decimal

Environment & setup

  • OS: macOS Ventura
  • Database: PostgreSQL
  • Node.js version: 16.14

Prisma information

model Order {
  id     Int          @id @default(autoincrement())
  amount    Decimal
}

Prisma Version

prisma                  : 4.6.1
@prisma/client          : 4.6.1
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 694eea289a8462c80264df36757e4fdc129b1b32 (at ../../node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 694eea289a8462c80264df36757e4fdc129b1b32 (at ../../node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 694eea289a8462c80264df36757e4fdc129b1b32 (at ../../node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 694eea289a8462c80264df36757e4fdc129b1b32 (at ../../node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Format Wasm             : @prisma/prisma-fmt-wasm 4.6.1-3.694eea289a8462c80264df36757e4fdc129b1b32
Default Engines Hash    : 694eea289a8462c80264df36757e4fdc129b1b32
Studio                  : 0.476.0
Preview Features        : interactiveTransactions, metrics

Issue Analytics

  • State:open
  • Created 10 months ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

1reaction
jon-ressiocommented, Dec 14, 2022

Decimal.js 10.4.3 includes the PR removing toStringTag from the typescript type definition. If Prisma can update their Decimal.js dependency and you can update the direct Decimal.js dependency, that should resolve this issue.

1reaction
lorenz-hcommented, Nov 30, 2022

I’m running into this same issue. My setup is a nextjs app, prisma -> trpc -> superjson -> react.

I’m registering a custom converter for Decimals with superjson and have added a Decimal.js package dependency (10.4.2 and 10.4.1 behave the same)

I initially tried using Decimal from my React components as:

import { Decimal } from "@prisma/client/runtime";

but the prisma client/runtime package seems to reference the ‘fs’ package so I get this error in the browser:

Module not found: Can't resolve 'fs'

It would be nice if the Decimal.js bundled with Prisma could be exported cleanly (from “@prisma/client/model” or whatever makes sense) so it can be imported from browser-side code. Alternatively, if Decimal.js removes toStringTag from their typescript declaration then this problem goes away.

@jon-ressio thanks for putting together the PR - Regarding your (I believe unrelated) import issue. Have you considered importing Decimal using:

import { Prisma } from "@prisma/client"

const d: Prisma.Decimal = new Prisma.Decimal(12)

However, the duplicate declaration of toStringTag leading to incompatibility will still persist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Option to disable Decimal.js Generation? · Issue #6049 - GitHub
I have few columns in my db that have Decimal values, my GraphQL Server usually returns whatever responses that Prisma returns (using Float...
Read more >
Error: Type 'number' is not assignable to type 'Decimal'
Type 'number' is not assignable to type 'Decimal'. How to convert javascript number to Prisma Decimal type? prisma.
Read more >
Prisma schema API (Reference)
API reference documentation for the Prisma Schema Language (PSL).
Read more >
inconsistent column data: failed to convert ... - You.com
When you have a introspected schema with type Decimal (E.g. via https://docs.atlas.mongodb.com/sample-data/sample-airbnb/), you get an error like this when you ...
Read more >
Prisma 2.17.0 Release - GitClear
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, ... Prisma schema to the database for Float has changed from Decimal to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found