Defining a Schema with Conditional Requirements in TypeScript

What's the right way to define a Schema like this so that typescript understands that if one value is undefined the other must be defined? Would I have to define a union and repeat all the fields in each variation?

S.Struct({
  periods: S.optional(S.Positive.pipe(S.int())),
  since: S.optional(S.String.pipe(S.pattern(/^\d{4}-\d{2}-\d{2}$/))),
  periodType: S.optional(S.Union(S.Literal("week"), S.Literal("month"))),
}).pipe(
  S.filter(
    ({ periods, since }) => periods !== undefined || since !== undefined,
    { message: () => "At least one of periods or since must be provided" }
  )
)
Was this page helpful?