typescriptprisma

does typescript can defind decimal type?


in the database (MySQL) the price is defined as decimal, then I use tRPC to fetch data, when I want to use the value it show errors.

 Types of property 'price' are incompatible.
    Type 'Decimal | null' is not assignable to type 'number'.
      Type 'null' is not assignable to type 'number'.

I try to find what type can I define decimal but only found number.

prisma:

model project{
  price  Decimal?  @db.Decimal(19, 4)
...
}

frontend

type projectType = {
price:number
...
}

I try use BigInt still error
Type 'Decimal | null' is not assignable to type 'BigInt'.

tempoary use any type to skip the error. how to solve this problem?

edit

  price  Decimal  @db.Decimal(19, 4)

remove the question mark to fix the null problem, but the error is same.


Solution

  • I found answers from Error: Type 'number' is not assignable to type 'Decimal'

    import { Prisma } from '@prisma/client'
    
    type projectType = {
    price:Prisma.Decimal
    ...
    }
    

    and the doc here "Decimal"