@@ -76,12 +76,15 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
7676
7777 protected emitImports ( ) : void {
7878 this . ensureBlankLine ( ) ;
79- this . emitLine ( this . importStatement ( "* as S" , '"@ effect/schema /Schema"' ) ) ;
79+ this . emitLine ( this . importStatement ( "* as S" , '"effect/Schema"' ) ) ;
8080 }
8181
8282 private typeMapTypeForProperty ( p : ClassProperty ) : Sourcelike {
83- const typeMap = this . typeMapTypeFor ( p . type ) ;
84- return p . isOptional ? [ "S.optional(" , typeMap , ")" ] : typeMap ;
83+ if ( ! p . isOptional ) {
84+ return this . typeMapTypeFor ( p . type ) ;
85+ }
86+
87+ return [ "S.optional(" , this . typeMapTypeFor ( p . type ) , ")" ] ;
8588 }
8689
8790 private typeMapTypeFor ( t : Type , required : boolean = true ) : Sourcelike {
@@ -104,13 +107,25 @@ export class TypeScriptEffectSchemaRenderer extends ConvenienceRenderer {
104107 _stringType => "S.String" ,
105108 arrayType => [ "S.Array(" , this . typeMapTypeFor ( arrayType . items , false ) , ")" ] ,
106109 _classType => panic ( "Should already be handled." ) ,
107- _mapType => [ "S.Record(S.String, " , this . typeMapTypeFor ( _mapType . values , false ) , ")" ] ,
110+ _mapType => [ "S.Record({ key: S.String, value: " , this . typeMapTypeFor ( _mapType . values , false ) , "} )" ] ,
108111 _enumType => panic ( "Should already be handled." ) ,
109112 unionType => {
110- const children = Array . from ( unionType . getChildren ( ) ) . map ( ( type : Type ) =>
111- this . typeMapTypeFor ( type , false )
112- ) ;
113- return [ "S.Union(" , ...arrayIntercalate ( ", " , children ) , ")" ] ;
113+ const types = Array . from ( unionType . getChildren ( ) ) ;
114+ let children : Sourcelike [ ] = [ ] ;
115+ let nullable = false ;
116+ for ( const type of types ) {
117+ if ( type . kind === "null" ) {
118+ nullable = true ;
119+ } else {
120+ children . push ( this . typeMapTypeFor ( type , false ) ) ;
121+ }
122+ }
123+
124+ if ( nullable && children . length === 1 ) {
125+ return [ "S.NullOr(" , children [ 0 ] , ")" ] ;
126+ }
127+
128+ return [ "S.Union(" , ...arrayIntercalate ( ", " , children ) , nullable ? ", S.Null)" : ")" ] ;
114129 } ,
115130 _transformedStringType => {
116131 return "S.String" ;
0 commit comments