Skip to content

Built-in way to coerce null to undefined #2103

Discussion options

You must be logged in to vote

A little cleaner way to do this is:

const schema = z.string().nullish().transform( x => x ?? undefined )
type Data = z.infer<typeof schema>
// type Data = string | undefined

console.log( schema.parse( 'foo' ) ) // 'foo'
console.log( schema.parse( null ) ) // undefined
console.log( schema.parse( undefined ) ) // undefined

Because this is quite a simple and clean way to do this. I'm not sure we need a built-in way to do this. Thoughts?

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@sschneider-ihre-pvs
Comment options

@JacobWeisenburger
Comment options

Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
1 reply
@JacobWeisenburger
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants
Converted from issue

This discussion was converted from issue #2070 on February 26, 2023 00:56.