-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON<T> utility type #48697
Comments
It's possible to write this type in userland; we don't add type aliases unless they're required for declaration emit since people have different opinions about the "right" way to declare these types. |
I have written a utility type for this, but it's not possible to write a robust one in userland, for a few reasons:
The current situation is not great when I get records with createdAt/updatedAt from Prisma and serialize with Remix/Next.js. I lose type safety as the Superjson is a solution like I mentioned, but you pay the price in more dependencies shipped to the client, serialization/deserialization runtime cost, and larger JSON payload. Since we already have an |
@frontsideair The |
@MichaelDeBoey I tried to do a thorough research to find such a helper so didn't have to write one myself, but I seem to have somehow missed that. 😅 I need to do some testing (there are a lot of edge cases!) but it looks promising! By the way, regardless of my evaluation, there's no way it can solve the |
@frontsideair I would suggest you to add an issue on the repo for that then. |
|
I tried my best to support all edge cases, but the issues I mentioned still remain, mainly the |
This issue has been marked as "Out of Scope" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Hi @frontsideair, I've found your comment by chance while bumping into this TypeScript issue myself. See docs: https://remix.run/docs/en/main/guides/single-fetch#streaming-data-format. |
@jkomyno yeah I’ve been following closely and pretty excited about this! Up until now I happily used |
Suggestion
🔍 Search Terms
json, jsonify
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
A
JSON<T>
utility type, whereT
is a record type of any shape and the output would be what you would get fromJSON.parse(JSON.stringify(t))
.I would expect that
Map
andSet
would be gone,Date
would turn intoString
etc.📃 Motivating Example
In full-stack frameworks like Next.js or Remix, you usually have a method for fetching data from the database to provide it to your route components. When doing a client-side transition, you need to call this method and transport using JSON. This is why Next.js has a lint rule for returning JSON-unsafe data from
getServerSideProps
.Using this utility type you could ignore this warning and work with whatever you get from the JSON in the component.
💻 Use Cases
Using this utility type you could ignore the warning and work with whatever you get from the JSON in the component.
My current alternative is using superjson as a babel plugin.
The text was updated successfully, but these errors were encountered: