Prisma.JsonValue type is incompatible with Typescript JSON.parse typing
See original GitHub issueHow 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:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.
No worries! Should I go ahead and close this issue?
json as any
orjson as string
, doesn’t matter. TypescriptJSON.parse
definition isn’t too helpful, it’s not even generic.This issue is just about not having to type
as string
each time.