`pick`, `omit`, `require` & `partial` should use arrays instead of objects
See original GitHub issueI 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:
- Created 10 months ago
- Reactions:3
- Comments:11 (3 by maintainers)
Top 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 >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
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, andconst
doesn’t fix that unfortunately.Too much of a footgun. Many many people will inevitably try this:
Which is bad because Zod can’t properly type the resulting schema, since the type signature of
keys
has collapsed tostring[]
.A similar problem exists for
z.enum
and it’s caused a lot of headaches both for users and maintainers.