Add option to generate unions for mutually exclusive properties
A schema can use not to forbid setting two of its properties together. This is how protobuf-to-OpenAPI converters encode a oneof. Because every not generates any, the members come out as independent optional properties, so TypeScript accepts objects that set several of them. This PR adds an opt-in exclusivePropertiesFromNot option. It generates such a group as a union in which only one member can be set.
Example
ComplianceRequest:
type: object
additionalProperties: false
properties:
reference: { type: string }
kyc: { type: string }
kyb: { type: string }
not:
required: [kyc, kyb]
Before, and still the default:
export type ComplianceRequest = {
reference?: string;
kyc?: string;
kyb?: string;
};
With exclusivePropertiesFromNot: true:
export type ComplianceRequest = (
| {
kyc: string;
kyb?: never;
}
| {
kyc?: never;
kyb: string;
}
| {
kyc?: never;
kyb?: never;
}
) & {
reference?: string;
};
{ kyc: "…", kyb: "…" } no longer type-checks, and members can still be read directly: request.kyc is string | undefined. A schema can also forbid setting none of the members:
not:
anyOf:
- required: [kyc, kyb]
- not: { anyOf: [{ required: [kyc] }, { required: [kyb] }] }
In that case the all-unset branch is dropped, so exactly one member must be set.
Option
The option is exclusivePropertiesFromNot in generateApi and --exclusive-properties-from-not on the CLI. It is off by default because it changes generated types, which matches how emptyNotAsNever was added. The changeset is a minor bump.
What is recognized
not is read as a list of forbidden cases. It can be a single case or several cases under not: { anyOf: [...] }. Three cases are recognized:
{ required: [a, b] }:aandbare both set.{ allOf: [{ anyOf: [{ required: [a] }, …] }, { not: { oneOf: [{ required: [a] }, …] } }] }: some of the names are set, but not exactly one. Large groups are written this way because the number of pairs grows quadratically.{ not: { anyOf: [{ required: [a] }, …] } }: none of the names is set.
A set of properties forms a group only when all three of these hold:
- the rules forbid every pair of its members together;
- no member is forbidden together with a property outside the group;
- every member is a declared property that is not in
required.
Each group becomes one union. The other properties stay in an object type that is intersected with the union(s). Everything else generates the same output as before, and a type is narrowed only by rules it expresses exactly. That includes:
- overlapping pairs (
a/bandb/c); { required: [a, b, c] }, which forbids only all three together;- names that are not declared properties;
- every other
not.
Implementation:
src/schema-parser/util/exclusive-properties.tsfinds the groups.NotSchemaParsergenerates one union per group. Each branch is an inline object schema in which the other members arefalseschemas. Member docs,$refs andadditionalPropertiestherefore render as they do anywhere else.ComplexSchemaParserleaves the grouped properties out of the object it intersects with the result, so they are not generated twice.
Tests
tests/spec/not-exclusive-propertiescovers:- an optional pair and a required pair;
- two groups in one schema;
- a three-member group;
- the five-member "some but not exactly one" form, with and without the "none set" rule;
- a one-member required group;
- three schemas that stay unchanged: overlapping pairs, a three-name
required, and an undeclared name.
- The option-on test snapshots the output. It then type-checks the output together with assignments marked
@ts-expect-error, such as two members set or a required group with no member set. With the option off, those directives are unused and the check fails. The default test checks that noneveris generated. vitest run: 314 tests in 105 files pass (312 in 104 before), with no snapshot changes outside the new spec.biome checkandtsc --noEmitreport the same diagnostics as onmain.
I also ran the option on a 740-operation document generated from protobuf. It has 57 schemas that use this encoding: pairs, required groups, and two groups of 63 and 144 members in the "some but not exactly one" form. With the option on, only the types of those 57 schemas change. Before this option, we got these types by rewriting the same rules into explicit oneOf branches before generation. tsc confirms that each of the 56 component types is mutually assignable with the rewritten one and that every member is still accessible on it. The 57th schema is an inline request body; its output differs from the rewritten one only in the order of the intersection.
🤖 Generated with Claude Code
Summary by cubic
Adds an opt-in exclusivePropertiesFromNot option that turns not schemas making object properties mutually exclusive into TypeScript unions, so assigning two members at once no longer type-checks.
- Generates one union branch per member with the others marked
never; an all-unset branch is added unless the schema also forbids setting none. - Recognizes forbidden pairs and the "some set, but not exactly one" form, both alone and grouped under
not: { anyOf }; overlapping pairs, largerrequiredlists, and undeclared names keep generating the same output as before. - Leaves out group members that are required at the property level (
x-omitempty: false, booleanrequired) and keeps the grouped properties in the object when a customcomplexNotparser is used. - Adds
--exclusive-properties-from-notto the CLI and leaves the option off by default, so existing generated types are unchanged unless it is enabled.
Migration
Existing output is unaffected. Enable the new option only if you want the new union types.