arktypea
arktype16mo ago
francis

How would you define an object with optional keys, but require at least one of the keys to be filled

An example of a very basic schema:
const schema = type({ a: type.string.optional(), b: type.string.optional() });


I'd like this to accept values {a: 'foo'}, { b: 'foo' }, and { a: 'foo', b: 'bar' }, but fail on {}. It would be a lovely bonus for the type to be inferred as { a: string, b?: string } | { a?: string, b: string } as well. Is there a way to do this without explicitly specifying that inferred type as an arktype union using .or?
Was this page helpful?