zod icon indicating copy to clipboard operation
zod copied to clipboard

add never props to safe parse return types

Open schicks opened this issue 4 months ago • 4 comments

Possible improvement to safe parse return types as discussed in #3266.

The ?: never properties added to the type involve no runtime overhead and support all existing patterns. However, as seen in the tests they avoid casting or intermediate variables when we attempt to access properties on the union SafeParseReturnType, since now both branches have the property we attempt to access. When accessing SafeParseReturnType['data'], we get Output | undefined, which is accurate to the runtime behavior. If we are in a context where we've already checked success, we get just Output as expected.

schicks avatar Mar 02 '24 15:03 schicks

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
Latest commit d6074be2c1aaeab1b04669c483c21e62596e3c6f
Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/65e34062f9690f0008bf86c1
Deploy Preview https://deploy-preview-3295--guileless-rolypoly-866f8a.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Mar 02 '24 15:03 netlify[bot]

@colinhacks does this seem like something we could add? Happy to make additional changes if necessary, but the basic functionality comes up fairly frequently when using zod in an environment where there needs to be a "default" flow (eg, react components handling network responses).

schicks avatar Mar 06 '24 14:03 schicks

@colinhacks does this seem like something we could add? Happy to make additional changes if necessary, but the basic functionality comes up fairly frequently when using zod in an environment where there needs to be a "default" flow (eg, react components handling network responses).

@JacobWeisenburger

somecodingwitch avatar Mar 06 '24 21:03 somecodingwitch

Seems interesting, but I am not in a position to make any decisions about these types of things. @colinhacks is the owner and would be the person to approve or not.

JacobWeisenburger avatar Mar 06 '24 21:03 JacobWeisenburger

Thanks @schicks, this is an excellent idea.

colinhacks avatar Mar 19 '24 23:03 colinhacks

This has landed in Zod 3.23.

https://github.com/colinhacks/zod/releases/tag/v3.23.0

colinhacks avatar Apr 21 '24 22:04 colinhacks