Skip to content

Are there negation types/operators? #926

Discussion options

You must be logged in to vote

There isn't a concept of xor or not that I know of, but you could do your example as shown below:

const optionalNonEmptyString = z.string().min( 1 ).optional()
type OptionalNonEmptyString = z.infer<typeof optionalNonEmptyString>
// type OptionalNonEmptyString = string | undefined

console.log( optionalNonEmptyString.safeParse( 'foo' ).success ) // true
console.log( optionalNonEmptyString.safeParse( undefined ).success ) // true
console.log( optionalNonEmptyString.safeParse( '' ).success ) // false

For other schemas that you want to use not, you could do something using refine.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants