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.

`pick`, `omit`, `require` & `partial` should use arrays instead of objects

See original GitHub issue

I was wondering what the rationale behind pick, omit, require and partial expecting objects is?

Each value is optional and can only be true, so using

.pick(['foo', 'bar'])

feels more sensible than

.pick({ foo: true, bar: true })

There is another reason than convenience:
Currently you can (accidentally) add properties, which do not exist on the original object.
With an array you could ensure type safety.

The signature would look like this

pick<Mask extends Array<keyof T>>(mask: Mask): ZodObject<Pick<T, Extract<keyof T, keyof Mask>>, UnknownKeys, Catchall>;

If you agree I could offer to implement this. Either in a backwards compatible way (also accepting objects, using overloads) or as a breaking change.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
colinhackscommented, Dec 15, 2022

This syntax is already implementable without const, that’s not the problem. It’s just a footgun that will cause users frustration. This is the part that makes this syntax a footgun, and const doesn’t fix that unfortunately. Screenshot 2022-12-14 at 7 28 37 PM

2reactions
colinhackscommented, Dec 14, 2022

Too much of a footgun. Many many people will inevitably try this:

const keys = ['a', 'b'];

const schema = z.object({
  a: z.string(),
  b: z.string(),
  c: z.string()
}).pick(keys); // bad

Which is bad because Zod can’t properly type the resulting schema, since the type signature of keys has collapsed to string[].

A similar problem exists for z.enum and it’s caused a lot of headaches both for users and maintainers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Utility Types - TypeScript
Utility Types · Awaited<Type> · Partial<Type> · Required<Type> · Readonly<Type> · Record<Keys, Type> · Pick<Type, Keys> · Omit<Type, Keys> · Exclude<UnionType, ...
Read more >
Can I have lodash omit return a specific type instead of a ...
Weird that omit doesn't infer types on its own. I'm not sure if there's a reason for that, but I was able to...
Read more >
11 Awesome TypeScript Utility Types You Should Know
11 Awesome TypeScript Utility Types You Should Know · Pick<Type, Keys> · Omit<Type, Keys> · Readonly<Type> · Partial<Type> · Required<Type> · Record< ...
Read more >
you-dont-need/You-Dont-Need-Lodash-Underscore - GitHub
List of JavaScript methods which you can use natively + ESLint Plugin - GitHub - you-dont-need/You-Dont-Need-Lodash-Underscore: List of JavaScript methods ...
Read more >
Advanced TypeScript Types Cheat Sheet (with Examples)
Let's dive in. Intersection Types; Union Types; Generic Types; Utility Types; Partial; Required; Readonly; Pick; Omit; Extract; Exclude; Record ...
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