JSON utility type
See original GitHub issueI just found myself needing a generic type for any valid JSON and put this together:
type Literal = boolean | null | number | string;
type Json = Literal | { [key: string]: Json } | Json[];
const Literal = Zod.union([Zod.boolean(), Zod.null(), Zod.number(), Zod.string()]);
const Json: Zod.ZodType<Json> = Zod.lazy(() =>
Zod.union([Literal, Zod.array(Json), Zod.record(Json)])
);
Putting this in an issue for anyone who might be on the lookout or if the library might want to integrate a higher-level utility constructs like this.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
JSON<T> utility type · Issue #48697 · microsoft/TypeScript
A JSON<T> utility type, where T is a record type of any shape and the output would be what you would get from...
Read more >JSON Serialization - Unity - Manual
Use the JsonUtility class to convert Unity objects to and from the JSON format. For example, you can use JSON Serialization to interact...
Read more >Documentation - Utility Types - TypeScript
TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally.
Read more >JSON Type Definition
JSON Type Definition is a lightweight schema language for JSON data. Describe the shape of your data once, and get portable validators and...
Read more >MakeTypes from JSON samples - John Vilk
MakeTypes generates TypeScript classes that parse and typecheck JSON objects at runtime, and let you statically type check code that interacts with JSON ......
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
Now that the requirement is 4.1+, any chance
z.json()
gets revisited?Yikes! When I copied the snippet above into the README I renamed
Json
tojsonSchema
(to avoid confusion with the built-inJSON
class) but I forgot to change it everywhere 😬 Here’s the correct snippet:Thanks for pointing this out! I fixed this in the README as well.