You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that z.omit and z.pick allow any keys to be omitted/picked, as long as one of the omitted/picked keys is in the object.
import{z}from"zod"exportconstSomeZodObject=z.object({a: z.string(),b: z.string(),})exportconstErroneousOmit=SomeZodObject.omit({c: true,// <-- Errors because `c` is not in `SomeZodObject`, and it is the only key being omitted})exportconstAllowedOmit=SomeZodObject.omit({a: true,c: true,// <-- Does *not* error because `a` *is*` part of `SomeZodObject`, so any key is now allowed})
The same behavior as above happens with pick.
Note that TypeScript's Omit type does allow omitting keys not present in the object:
typeSomeZodObjectInput=z.input<typeofSomeZodObject>// No errorsexporttypeTSOmitted1=Omit<SomeZodObjectInput,"c">exporttypeTSOmitted2=Omit<SomeZodObjectInput,"a"|"c">
But TypeScript's Pick type does not:
typeSomeZodObjectInput=z.input<typeofSomeZodObject>// Both of these errorexporttypeTSPicked1=Pick<SomeZodObjectInput,"c">exporttypeTSPicked2=Pick<SomeZodObjectInput,"a"|"c">
Is this intended behavior? And if so, is there a way to get TS to error for any key that doesn't exist in the object?
It seems that
z.omit
andz.pick
allow any keys to be omitted/picked, as long as one of the omitted/picked keys is in the object.The same behavior as above happens with
pick
.Note that TypeScript's
Omit
type does allow omitting keys not present in the object:But TypeScript's
Pick
type does not:Is this intended behavior? And if so, is there a way to get TS to error for any key that doesn't exist in the object?
TSPlayground: https://tsplay.dev/wO1pdm
The text was updated successfully, but these errors were encountered: