Skip to content

Commit bc4d34d

Browse files
committed
Fix complex OneOf schemas
1 parent de9792f commit bc4d34d

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

packages/openapi-typescript/src/utils.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@ export function tsNonNullable(type: string): string {
180180
return `NonNullable<${type}>`;
181181
}
182182

183-
/** OneOf<T> (custom) */
183+
/**
184+
* OneOf<T>
185+
* TypeScript unions are not exclusive @see https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-types
186+
* However, at a certain size, the helper type becomes to complex for inference to work. Hence the > check.
187+
*/
184188
export function tsOneOf(...types: string[]): string {
185-
if (types.length === 1) return types[0];
189+
if (types.length === 1) {
190+
return types[0];
191+
} else if (types.length > 5) {
192+
return tsUnionOf(...types);
193+
}
186194
return `OneOf<[${types.join(", ")}]>`;
187195
}
188196

0 commit comments

Comments
 (0)