Branded types
See original GitHub issueFeature request: branded types This issue can be the primary discussion ground for implemented branded types.
Option 1: Replicating io-ts’s symbol trickery to create types like NonZeroNumber or Email (it might be difficult to do that one while remaining ergonomic)
I don’t love all the boilerplate associated with io-ts
branded types (i.e. interface PositiveBrand { readonly Positive: unique symbol }
).
Option 2
It may be able to include string/number validators as a literal generic argument of the ZodString/ZodNumber classes, like so:
const num: z.number({ max: 5 }) // => z.ZodString
const max5: z.number({ max: 5 }) // => z.ZodString<{ max: 5 }>
That way the validations being enforced are easy to see with a glance at the type definition. This would only work for built-in validators I believe (?). This is also different, in that validations are registered at the instance level instead of the class level.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:10 (3 by maintainers)
This is the recommended way to do tagged/branded types in TypeScript/Zod:
Is there any way without using any?