zod
zod copied to clipboard
add never props to safe parse return types
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.
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...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
@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).
@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
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.
Thanks @schicks, this is an excellent idea.
This has landed in Zod 3.23.
https://github.com/colinhacks/zod/releases/tag/v3.23.0