@@ -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