Generic for parsing objects
See original GitHub issueReally enjoy the library! Thank you for the work!
To easier debug when a parse fails, I made a small wrapper like this:
function parse(parser, obj) {
try {
return parser.parse(obj);
} catch (e) {
console.log(obj);
throw e;
}
}
The problem is that typescript can not infer this function.
I tried multiple combinations but can not seem to get it right.
I looked through issues already mentioning generic and did fine one small example: https://github.com/vriad/zod/issues/93 but it did not help fully.
function parse<K, T extends z.ZodType<K, any>>(parser: T, obj): K {
try {
return parser.parse(obj);
} catch (e) {
console.log(obj);
throw e;
}
}
function parse<T extends z.ZodType<any, any>, K typeof T>(parser: T, obj): K {
try {
return parser.parse(obj);
} catch (e) {
console.log(obj);
throw e;
}
}
interface Schema<T> {
parse: (data:unknown): T;
check: (data:unknown): data is T;
}
function parse<T extends Schema<K>>(parser: T, obj) {
try {
return parser.parse(obj);
} catch (e) {
console.log(obj);
throw e;
}
}
interface SchemaVal {
parse<K>(data: unknown): K;
}
function parse<T extends SchemaVal>(parser: T, obj): K {
try {
return parser.parse(obj);
} catch (e) {
console.log(obj);
throw e;
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:8 (8 by maintainers)
Top Results From Across the Web
How to parse a json to object with a list of generic? [duplicate]
I'd like to convert an object into json and then parse the json back to the original object. The difficulty is there are...
Read more >Example Using Generic (JSON) Resource Objects - Bloomreach
Resource is the primary object representation to solve all the complex integration problems in a generic way. So, the interface was designed to...
Read more >Parsing JSON to Generic Class Object with Gson
Parsing JSON to Generic Class Object with Gson ... We can parse this JSON string into a String array : Gson gson =...
Read more >MS | Generic JSON Parsing - Mark Struzinski
I'll present a simple generic object and its associated json in this post, and then show how that expanded out to a pretty...
Read more >Generic JSON parsing using Protocol Oriented Programming
The main usage of Parseable object is based on injecting Parseable concrete instance to an asynchronous (escaping closure) generic method that ...
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
Hey @kevinsimper -
Good question. Generics are very fussy. I need to do a better job documenting how to write generic helper functions like this. Here’s how to do what you want:
Though I think a “parse factory” pattern would be even better so you don’t need to keep passing the schema as an argument to parse:
Then you can use it like this:
I have written this which has somewhat different goals as the above example, which may be of interest to people:
Usage: