Better typescript support for JSON fields

Hi. I have a Prisma model with a JSON field (backed by jsonb type for postgres). As far as the React<–>GraphQL<–>Prisma<–>DB dataflow goes, it’s all working fine. The problem is the typescript support is not great.

  1. I have custom type definitions for this column that I use in my app, let’s call that MyType
  2. On the React/client side of things, this comes up as Record<string, unknown> so I cast it to MyType with an as statement wherever I use it
  3. On the api side of things, Prisma returns a Prisma.JsonValue for it, and graphQL still thinks it’s a Record<string, unknown> and they are not really compatible with each other without casting both as an object (since I don’t have my custom typedefs available on the api side).

My questions are:

  • Is there a good way to tell all the involved systems to treat this as MyType
  • Failing that, is there a way to have my custom typedefs available on both the web and api sides?

Thanks

I haven’t tried it myself, but I think it should work to create a /types directory at the root (same level as /web and /api. Looking at the tsconfig files for both web and api it looks like they’ll include anything you place there.

1 Like