Doubts about type inference when using picks
See original GitHub issueHello everyone, when calling the pick method of a zodObject
in a function by passing in a reference, how to set the type of T
to ensure the correct type of return value?
function zodPickDemo<T extends ZodObject<ZodRawShape>>(src: T) {
return src.pick({ id: true });
}
const dto = z.object({ id: z.string(), name: z.string() });
const picked = zodPickDemo(dto);
// In this case the type of picked is not the expected type
In the above code, the type expected to be obtained is a zodObject
object type containing only the id, but what is obtained is a type such that:
z.ZodObject<Pick<z.ZodRawShape, never>, "strip", z.ZodTypeAny, {}, {}>
Playground: link
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Doubts about type inference when using pick of zodObject
Save this question. Show activity on this post. when calling the pick method of a zodObject in a function by passing in a...
Read more >Type inference cannot properly infer type for object literal ...
TypeScript Version: 2.6.0 and 2.7.0-dev.20171114 Code declare namespace Module { class MyClass { // template functions using Pick foo (o: ...
Read more >programming languages - What's the tradeoff for type inference?
Languages with opt-out type inference strike an agreeable balance between the two ... functions with the same name, and it picks the one...
Read more >Lectures 5 and 6: Type Inference
We will solve the type inference problem using constraint-based typing judgements and a unification algorithm. The constraint-based typing ...
Read more >Local Type Inference - UPenn CIS
We study two partial type inference methods for a language combining subtyping and impredica- tive polymorphism. Both methods are local in the sense...
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
I managed to get the correct functionality with this code:
Does this solve your problem?
Thanks @AustinShelby for providing the working code, that does seem to be the correct way to address the original question.