Skip to content

Commit b2eaef4

Browse files
committed
fix: avoid double ups of null and/or undefined in types
1 parent cdc21f6 commit b2eaef4

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

__tests__/fixtures/test1/models.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* WARN: Do not edit directly.
55
*
6-
* Generated on 2023-04-01T07:54:08.728Z
6+
* Generated on 2023-04-01T08:21:57.383Z
77
*
88
*/
99
export type Uuid = string;
@@ -130,19 +130,19 @@ export type BillingAccountCreateRequest = {
130130
country: BillingCountry;
131131
timeZone: TimeZone;
132132
currency: Currency;
133-
locale?: BillingLocale | undefined | null | undefined;
134-
purchaseOrder?: StringU8 | undefined | null | undefined;
135-
taxId?: StringU8 | undefined | null | undefined;
133+
locale?: BillingLocale | null | undefined;
134+
purchaseOrder?: StringU8 | null | undefined;
135+
taxId?: StringU8 | null | undefined;
136136
};
137137
export type BillingAccountUpdateRequest = {
138138
name?: Name | undefined;
139139
email?: Email | undefined;
140140
country?: BillingCountry | undefined;
141141
timeZone?: TimeZone | undefined;
142142
currency?: Currency | undefined;
143-
locale?: BillingLocale | undefined | null | undefined;
144-
purchaseOrder?: StringU8 | undefined | null | undefined;
145-
taxId?: StringU8 | undefined | null | undefined;
143+
locale?: BillingLocale | null | undefined;
144+
purchaseOrder?: StringU8 | null | undefined;
145+
taxId?: StringU8 | null | undefined;
146146
};
147147
export type BillingAccountList = BillingAccount[];
148148
export type BillingAccountPortalRequest = {

lib/process-schema.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ export function schemaToType(
184184

185185
const types = schemaItems
186186
.map((schema) =>
187-
schemaToType(typesAndInterfaces, parentSchema, propertyName, schema),
187+
schemaToType(typesAndInterfaces, parentSchema, propertyName, schema, {
188+
// forcibly disallow undefined, we will handle it later
189+
disallowUndefined: true,
190+
}),
188191
)
189192
.map((t) => t.type);
190193

@@ -203,7 +206,7 @@ export function schemaToType(
203206
};
204207
}
205208

206-
// already got a nullable type, no need to add null
209+
// already got a nullable type, no need to add another null
207210
if (types.some((t) => t === 'null')) {
208211
return {
209212
name,
@@ -242,7 +245,7 @@ export function schemaToType(
242245
return {
243246
name,
244247
hasQuestionToken: false,
245-
type: maybeWithUndefined('null', false),
248+
type: 'null',
246249
docs,
247250
};
248251
}
@@ -405,18 +408,27 @@ export function registerTypesFromSchema(
405408
}),
406409
);
407410

408-
const writerType = intersect
409-
? Writers.intersectionType.bind(Writers)
410-
: Writers.unionType.bind(Writers);
411+
// concat and dedupe
412+
const typeArgs = [
413+
...new Set([
414+
...typeAliases.map((t) => t.getName()),
415+
...objectTypesFromNonRefSchemas.filter(Boolean),
416+
]),
417+
];
418+
419+
const writerType = intersect ? Writers.intersectionType : Writers.unionType;
420+
421+
// gets around picky types for writerType
422+
const [firstType = 'never', secondType, ...restTypes] = typeArgs;
423+
411424

412425
const typeAlias = typesFile.addTypeAlias({
413426
name: pascalCase(schemaName),
414427
isExported: true,
415-
type: writerType(
416-
// @ts-expect-error -> bad type in ts-morph (arguably)
417-
...typeAliases.map((t) => t.getName()),
418-
...objectTypesFromNonRefSchemas,
419-
),
428+
type:
429+
firstType && secondType
430+
? writerType.call(Writers, firstType, secondType, ...restTypes)
431+
: firstType,
420432
});
421433

422434
if (schemaObject.description) {

0 commit comments

Comments
 (0)