Union Types (anyOf)
Members that accept more than one type, via anyOf.
When a field must accept values of more than one type, use the anyOf constraint on the any type (see Any). A value is valid if it matches any one of the listed alternatives.
id: { any, anyOf: [string, int] }
---
~ 42 # ✓ matches int
~ abc # ✓ matches stringA value matching none of the alternatives is rejected:
flag: { any, anyOf: [bool, int] }
---
~ T # ✓
~ hello # ✗ matches neitherConstrained and structured alternatives
Each alternative may be a full MemberDef (with constraints) or an object shape, not just a bare type name:
value: { any, anyOf: [ { int, multipleOf: 5 }, { int, multipleOf: 3 } ] }
---
~ 10 # ✓ multiple of 5
~ 9 # ✓ multiple of 3Guidance
Order alternatives from most specific to least specific.
Prefer
anyOfover a bareanywhen the set of acceptable types is known — it keeps validation meaningful.
See Also
Last updated
Was this helpful?
