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.JsonValue type is incompatible with Typescript JSON.parse typing

See original GitHub issue

How to reproduce

import { Prisma } from "@prisma/client";

function parsePrisma(json: Prisma.JsonValue) {
  JSON.parse(json); 
  // Argument of type 'JsonValue' is not assignable to parameter of type 'string'.
  // Type 'null' is not assignable to type 'string'.
}

Expected behavior

It’s arguable that Prisma’s typing is better than Typescript’s - JS JSON.parse indeed accepts nulls, numbers etc. I would still expect the typings to match, so that I don’t have to typecast every time I parse the database response.

Prisma information

2.20.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aqrlncommented, Apr 16, 2022

Just checked, and at least in PostgreSQL the return is indeed an already parsed object. We migrated from MySQL to Postgre since then, so maybe it wasn’t the case with MySQL driver, or maybe our database was weirder then?

Oh, I see. That would sound like a bug if it returned a JSON string to you instead of an actual value. I can’t reproduce it with the latest Prisma with MySQL, but I wonder if something might have gone wrong back then.

I’d love a way to easily get stronger type on a Json field, but that’s a different topic altogether.

Yes, it’s a popular feature request (https://github.com/prisma/prisma/issues/3219), and we’d love to have this too, but I can’t share any specific timeline. Now that we support composite types for embedded documents in MongoDB we’re one step closer to typed JSON fields in relational databases as well though.

Sorry about the confusion, I guess?

No worries! Should I go ahead and close this issue?

1reaction
Faithfindercommented, Apr 14, 2021

json as any or json as string, doesn’t matter. Typescript JSON.parse definition isn’t too helpful, it’s not even generic.

parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;
// obviously dummy code
function parsePrisma(json: Prisma.JsonValue) {
  return JSON.parse(json as string) as WhatIExpectTheJsonToBe;
}

This issue is just about not having to type as string each time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Json fields (Concepts) - Prisma
Use the Json Prisma field type to read, write, and perform basic filtering on JSON types in the underlying database. In the following...
Read more >
Type for JSON field on Type-GraphQL - Stack Overflow
I'm defining a custom input field on type-graphql of type JSON. We're using Prisma as well. I tried with Prisma.JsonValue, Prisma.
Read more >
Zapatos: Zero-Abstraction Postgres for TypeScript - Jawj
Exactly the kind of 'use SQL in typescript code with type-safety' non-ORM that I've ... complex nested JSON structures, still fully and automatically...
Read more >
Prisma 2.11.0 Release - GitClear
Up to this release, the Prisma schema only allowed to represent a limited set of types: String , Int , Float , Boolean...
Read more >
A curated list of awesome Go frameworks, libraries and ...
ini - INI Parser & Write Library, Unmarshal to Struct,Marshal to Json,Write File,watch file. joshbetz/config - Small configuration library for Go that parses ......
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