@@ -482,6 +482,14 @@ export const apply = async ({
482
482
}`
483
483
: ''
484
484
485
+ function generateNullableUnionTsType ( tsType : string , isNullable : boolean ) {
486
+ // Only add the null union if the type is not unknown as unknown already includes null
487
+ if ( tsType === 'unknown' || tsType === 'any' || ! isNullable ) {
488
+ return tsType
489
+ }
490
+ return `${ tsType } | null`
491
+ }
492
+
485
493
function generateColumnTsDefinition (
486
494
schema : PostgresSchema ,
487
495
column : {
@@ -497,7 +505,7 @@ export const apply = async ({
497
505
views : PostgresView [ ]
498
506
}
499
507
) {
500
- return `${ JSON . stringify ( column . name ) } ${ column . is_optional ? '?' : '' } : ${ pgTypeToTsType ( schema , column . format , context ) } ${ column . is_nullable ? '| null' : '' } `
508
+ return `${ JSON . stringify ( column . name ) } ${ column . is_optional ? '?' : '' } : ${ generateNullableUnionTsType ( pgTypeToTsType ( schema , column . format , context ) , column . is_nullable ) } `
501
509
}
502
510
503
511
let output = `
@@ -537,7 +545,7 @@ export type Database = {
537
545
...schemaFunctions
538
546
. filter ( ( { fn } ) => fn . argument_types === table . name )
539
547
. map ( ( { fn } ) => {
540
- return `${ JSON . stringify ( fn . name ) } : ${ getFunctionReturnType ( schema , fn ) } | null `
548
+ return `${ JSON . stringify ( fn . name ) } : ${ generateNullableUnionTsType ( getFunctionReturnType ( schema , fn ) , true ) } `
541
549
} ) ,
542
550
] }
543
551
}
@@ -610,7 +618,7 @@ export type Database = {
610
618
. filter ( ( { fn } ) => fn . argument_types === view . name )
611
619
. map (
612
620
( { fn } ) =>
613
- `${ JSON . stringify ( fn . name ) } : ${ getFunctionReturnType ( schema , fn ) } | null `
621
+ `${ JSON . stringify ( fn . name ) } : ${ generateNullableUnionTsType ( getFunctionReturnType ( schema , fn ) , true ) } `
614
622
) ,
615
623
] }
616
624
}
@@ -709,12 +717,15 @@ export type Database = {
709
717
const type = typesById . get ( type_id )
710
718
let tsType = 'unknown'
711
719
if ( type ) {
712
- tsType = `${ pgTypeToTsType ( schema , type . name , {
713
- types,
714
- schemas,
715
- tables,
716
- views,
717
- } ) } | null`
720
+ tsType = `${ generateNullableUnionTsType (
721
+ pgTypeToTsType ( schema , type . name , {
722
+ types,
723
+ schemas,
724
+ tables,
725
+ views,
726
+ } ) ,
727
+ true
728
+ ) } `
718
729
}
719
730
return `${ JSON . stringify ( name ) } : ${ tsType } `
720
731
} ) }
0 commit comments