avatar must be a string (was undefined) although undefined it expected type
I have the following type:
This is working:
But this throws an
avatar must be a string (was undefined)
error:
although it should be in the expected output range:
but still, it throws an avatar must be a string (was undefined)
error 🤔8 Replies
Workaround: Adding the "undefined" keyword explicitly.
https://arktype.io/docs/primitives#undefined
But this limits the usage of
.partial()
. So, if there would be a way that .partial()
can do this automatically, this would be awesome.ArkType Docs
Optimized runtime validation for TypeScript syntax
Note this is also the behavior in TS with
exactOptionalPropertyTypes
:
Also I realize now my naming is terrible and confusing but pretend
string
is key
or something 😛You should keep an eye on this issue though as it seems like the behavior you want:
https://github.com/arktypeio/arktype/issues/1191 (fixed link)
GitHub
Add an option to allow
undefined
for optional properties · Issue ...Request a feature Add a global option to allow undefined as a value for optional properties. configure({ optionalAllowsUndefined: true }); 🤷 Motivation const arkUser = type({ phoneNumber: 'stri...
Ooo this should go on that “Zod users” migration page - Zod allows undefined in optional() (example)
Zod Playground
An interactive playground for testing and learning Zod schemas
I wonder if that will still be the default in v4 - @colinhacks was just posting about the meaningful distinction
https://x.com/colinhacks/status/1893974904984547373
Colin McDonnell (@colinhacks) on X
if I may be so bold, I'd like to propose some much-needed nomenclature

X
Interesting! I always did find it confusing how both no key and key set to undefined are treated the same in some code and not others (in general, not just within validation libs)
Yeah, the reason EOPT is so important is without it, you have no way at all to represent what could be a meaningful distinction
With it, you can express all variants of optionality, so a superset of what is possible without it