question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doubts about type inference when using picks

See original GitHub issue

Hello 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:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
AustinShelbycommented, Jul 3, 2022

I managed to get the correct functionality with this code:

import type { ZodObject, ZodTypeAny } from 'zod';
import { z } from 'zod';

function zodPickDemo<T extends ZodTypeAny>(src: ZodObject<{id: T}>): ZodObject<{id: T}> {
  return src.pick({ id: true });
}

const dto = z.object({ id: z.string(), name: z.string() });
const picked = zodPickDemo(dto);
picked.shape.id; // z.ZodString

Does this solve your problem?

0reactions
scotttrinhcommented, Nov 1, 2022

Thanks @AustinShelby for providing the working code, that does seem to be the correct way to address the original question.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found