zod: safeParse discriminated union doesn't have 'error' attribute
const parsedUser = userZ.safeParse(user.toObject())
if (!parsedUser.success) {
console.error(parsedUser.error)
return null
}
Gives me this error:
Property 'error' does not exist on type 'SafeParseReturnType<{ telegramId?: number; telegramHandle?: string; twitterId?: number; twitterHandle?: string; address?: string; ens?: string; telegramFirstName?: string; twitterUsername?: string; isMetabotEnabled?: boolean; }, { ...; }>'.
Property 'error' does not exist on type 'SafeParseSuccess<{ telegramId?: number; telegramHandle?: string; twitterId?: number; twitterHandle?: string; address?: string; ens?: string; telegramFirstName?: string; twitterUsername?: string; isMetabotEnabled?: boolean; }>'.ts(2339)
when success===true it doesn’t give an error when i try to access data. Example:
return parsedUser.success ? parsedUser.data : null
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 27
I found a solution:
This fails
This works
Perhaps the docs should update the example so users can infer the correct types.
I have strict: true, but I’m still seeing errors when doing
!result.success. I’m still having to do a manual comparison with===.I think the return values of the safeParse and its types needs to be make more consistent. I think both cases must have the same fields, just with different values, either them being the value or null.
I don’t know if there is any specific ts reason to not do it like this
Please open up, this issue is still active.
@scotttrinh
This does not seem like a closed issue and thus should be re-opened.
I pretty much think this is a good idea, library apis that always return same shape results are much easier to work with and reason about.
So I have made a wrapper for
safeParseproviding a predictable API to work with:Maybe some on find it useful too.
strictandstrictNullChecksdoesn’t really solve the issue that we can’t do early returns by just checkingif (result.error). it’s almost purely a stylistic difference, but it’s definitely unexpected and not at all “convenient” like the docs claimif anything it should get a more honest call out and say that because it’s a discriminated union, the usual early return syntax is reversed from what you normally see: you must check for a
falsesuccess, and not the existence of an errorI think safeParse should have the same results as your implementation. I made some changes in your function to keep the type inference.
Found the issue.
In order to do this,
if (!result.success), strictNullChecks must be set to true.Alternatively, you can do this:
if (result.success === false)or
@ytsruh
SafeParseReturnTypeis a discriminated union, so you have to narrow the type before accessing the properties of the result object. A few of the examples above show how it’s done, but here’s your same example:I’m also having a very similar issue to this & causing my build to fail.
ERROR: src/pages/contact.tsx:24:15 - error TS2339: Property ‘data’ does not exist on type ‘SafeParseReturnType<{ message?: string; name?: string; email?: string; }, { message?: string; name?: string; email?: string; }>’.
CODE:
const { data, error } = ContactForm.safeParse({ name, email, message });I took the code from the Zod documentation as well but it seems that the SafeParseReturnType doesn’t contain either a ‘data’ or ‘error’ key that I can look into
Hi, @Daniel-Griffiths! I am using
zod v3.14.5and have no issues using the following:I am having the same errors trying to access
address.dataon the last lineProperty 'data' does not exist on type 'SafeParseErrorProperty 'data' does not exist on type 'SafeParseReturnTypeDo you have
strict: truein yourtsconfig.json? I cannot reproduce the issue in the TypeScript playground:TypeScript Playground
What solved for me was to set
"strict": trueand"strictNullChecks": truein thetsconfig.jsonunder thecompilerOptions.The code below now works as expected (no TS complaints):
Currently using:
How is https://stackblitz.com/edit/typescript-baclo6?file=index.ts not a problem? To be fair I don’t know whether it’s typescript or zod issue, it exists, doesn’t it?
If
strict:falseintsconfig.jsonthen will this fail?