safeParse discriminated union doesn't have 'error' attribute
See original GitHub issueconst 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
Issue Analytics
- State:
- Created a year ago
- Comments:13
Top Results From Across the Web
TypeScript discriminated union does not give error for non ...
It just says "this property can never be set". Note that payload doesn't have to be an object. You asked about the difference...
Read more >Colin McDonnell on Twitter: "@mjackson Well I'm biased but ...
The `safeParse` method returns a discriminated union that discriminates based on the "success" property and either contains the ...
Read more >TypeScript Fundamentals - Joy of Code
A discriminated union is the result of narrowing the members of the union that have the same literal type. For example we can...
Read more >zod@v3.20.2 | Deno
Tuples; Unions; Discriminated Unions ... mySchema.parse(12); // => throws ZodError // "safe" parsing (doesn't throw error if validation fails) mySchema.
Read more >Validating typescript Lambda input with Zod
Then I'd also have to define a Typescript Type or Typescript Interface ... safeParse function that doesn't immediately throw an error if it ......
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
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===
.