question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

safeParse discriminated union doesn't have 'error' attribute

See original GitHub issue
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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13

github_iconTop GitHub Comments

13reactions
Daniel-Griffithscommented, Jun 30, 2022

I found a solution:

This fails

 const result = validator.safeParse(example);

if (!result.success) {
    result.error // this shows a TS error
}

This works

 const result = validator.safeParse(example);

if (result.success === false) {
    result.error // it works!
}

Perhaps the docs should update the example so users can infer the correct types.

4reactions
aaronmcadamcommented, Sep 14, 2022

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

if (!result.success) {
  const { error } = result as SafeParseError;

I have strict: true, but I’m still seeing errors when doing !result.success. I’m still having to do a manual comparison with ===.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found