A
arktype2w ago
Micha

avatar must be a string (was undefined) although undefined it expected type

I have the following type:
import { type } from "arktype";

export const profileData = type({
firstName: 'string',
lastName: 'string',
'avatar?': 'string'
});
export type ProfileData = typeof profileData.infer
import { type } from "arktype";

export const profileData = type({
firstName: 'string',
lastName: 'string',
'avatar?': 'string'
});
export type ProfileData = typeof profileData.infer
This is working:
const data = profileData.partial()({ firstName: 'John', lastName: 'Doe' });
const data = profileData.partial()({ firstName: 'John', lastName: 'Doe' });
But this throws an avatar must be a string (was undefined) error:
const data = profileData.partial()({
firstName: 'John',
lastName: 'Doe',
avatar: undefined
});
const data = profileData.partial()({
firstName: 'John',
lastName: 'Doe',
avatar: undefined
});
although it should be in the expected output range:
const data: ArkErrors | {
firstName?: string | undefined;
lastName?: string | undefined;
avatar?: string | undefined;
}
const data: ArkErrors | {
firstName?: string | undefined;
lastName?: string | undefined;
avatar?: string | undefined;
}
but still, it throws an avatar must be a string (was undefined) error 🤔
8 Replies
Micha
MichaOP2w ago
Workaround: Adding the "undefined" keyword explicitly.
export const profileData = type({
firstName: 'string',
lastName: 'string',
'avatar?': 'string | undefined'
});
export const profileData = type({
firstName: 'string',
lastName: 'string',
'avatar?': 'string | undefined'
});
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
ssalbdivad
ssalbdivad2w ago
Note this is also the behavior in TS with exactOptionalPropertyTypes:
No description
ssalbdivad
ssalbdivad2w ago
Also I realize now my naming is terrible and confusing but pretend string is key or something 😛
ssalbdivad
ssalbdivad2w ago
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...
Jacob
Jacob7d ago
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
ssalbdivad
ssalbdivad7d ago
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
From An unknown user
X
Jacob
Jacob7d ago
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)
ssalbdivad
ssalbdivad7d ago
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

Did you find this page helpful?